【发布时间】:2020-07-10 03:51:18
【问题描述】:
在我的子组件中有以下方法可以更新 prop 更改的状态,效果很好
componentWillReceiveProps(nextProps) {
// update original states
this.setState({
fields: nextProps.fields,
containerClass: nextProps.containerClass
});
}
我收到Warning: Using UNSAFE_componentWillReceiveProps in strict mode is not recommended and may indicate bugs in your code.
我尝试更新,但到目前为止没有任何成功
static getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.fields !== prevState.fields) {
return { fields: nextProps.fields };
}
}
componentDidUpdate(nextProps) {
console.log(nextProps);
this.setState({
fields: nextProps.fields,
containerClass: nextProps.containerClass
});
}
因为我进入了无限循环。
如何根据新道具正确更新我的状态
【问题讨论】:
标签: reactjs updates deprecated prop