【问题标题】:Native Base Picker generating "Unsafe legacy lifecycles"原生 Base Picker 生成“不安全的遗留生命周期”
【发布时间】:2019-12-10 11:07:03
【问题描述】:

我在 react native 中创建了一个非常简单的页面。但是,我收到了警告:

警告:将不会为使用新组件 API 的组件调用不安全的旧生命周期。

%s 使用 %s,但也包含以下遗留生命周期:%s%s%s

应删除上述生命周期。在此处了解有关此警告的更多信息: https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html, Styled(PickerNB), getDerivedStateFromProps(), , UNSAFE_componentWillReceiveProps,

这是因为原生的 Picker。如果我移除选择器,我不会收到警告。

...
class ChangeProperty extends Component {
   constructor(props) {
    super(props);
    this.state = {
      selectedProperty: '1'
    };
  }

  componentDidMount() {
    this.props.getProperties(); // It just loads a properties data from action component
  }

  onChangeProperty(value) {
    this.setState({
      selectedProperty: value
    });
  }

  updatePropertyBTN = async () => {
    await AsyncStorage.setItem('CurrentPropertyID', this.state.selectedProperty);
    NavigationService.navigate('iRent');
  }

  ...
  <Picker
            mode="dropdown"
            iosHeader="Select Property"
            placeholder="Property"
            iosIcon={<Icon name="arrow-down" />}
            selectedValue={this.state.selectedProperty}
            textStyle={{ color: '#C0C0C0' }}
            style={{ width: '100%' }}
            onValueChange={(text) => this.onChangeProperty(text)}
          >
          {Object.keys(this.props.properties).map((key) => {
              return (
                <Picker.Item
                  label={this.props.properties[key]}
                  value={key}
                  key={key}
                />
              );
          })}
   </Picker>
}

它没有导致我的代码出现任何错误,但终端中的警告消息让我感到不安,因为我不知道是什么原因造成的。

谢谢

【问题讨论】:

    标签: react-native native-base


    【解决方案1】:

    出现警告是因为 NativeBase 选择器似乎正在使用 React 不再支持的遗留生命周期方法(例如 componentWillReceiveProps,如警告中提到的) - 这与你的代码。

    确保您的 NativeBase 已更新到最新的软件包版本,如果是,您可以在他们的 repo here 上提出问题

    【讨论】:

    • 是的,我使用的是最新版本。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 2020-11-16
    • 1970-01-01
    • 2023-03-11
    • 2021-05-01
    • 1970-01-01
    相关资源
    最近更新 更多