【发布时间】: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>
}
它没有导致我的代码出现任何错误,但终端中的警告消息让我感到不安,因为我不知道是什么原因造成的。
谢谢
【问题讨论】: