在我的子组件中有以下方法,可以在属性更改时更新状态,这很好用。
componentWillReceiveProps(nextProps) {
// update original states
this.setState({
fields: nextProps.fields,
containerClass: nextProps.containerClass
});
}
我收到了警告:Warning: 在严格模式下使用 UNSAFE_componentWillReceiveProps 并不被推荐,因为这可能表明您的代码存在漏洞。
我试图进行更新,但到目前为止还没有成功。
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
});
}
因为我进入了无限循环。
如何根据新的props正确更新我的状态。
componentDidUpdate(prevProps, prevState, snapshot)
。我认为您可能需要重新审查您的逻辑。 - Pranithan T.