【发布时间】:2016-04-15 14:01:58
【问题描述】:
我正在使用 React (14.2) 和 Redux。我已经完成了所有设置并正常工作,除非我在更新时分派操作,否则会发生递归循环。
componentWillMount() {
const { wizardActions, valid, errors } = this.props
wizardActions.setPropState({valid: valid, errors: errors})
}
componentWillUpdate(nextProps) {
const { wizardActions } = this.props
console.log('NP', nextProps)
//*** IF I UNCOMMENT THE FOLLWOING LINES THE APP GOES INTO AN INFINTIE
// RECURSION LOOP...
/*if(this.props !== nextProps) {
wizardActions.setPropState({valid: nextProps.valid, errors: nextProps.errors})
}*/
}
当当前组件的 props 发生变化时,如何更新 Redux 状态?我也在componentWillReceiveProps() 中尝试过这个,它做了同样的事情。
TIA!
【问题讨论】:
-
使用
if声明?当然你只想在特定情况下更新后发送,例如this.props !== nextProps -
添加了 if 语句,但行为仍然相同
-
哦,这不可能是 exact if 语句,因为对象引用总是不同的.. 总是会评估为 true。您必须比较您想要成为新的道具中的特定值。
标签: javascript reactjs redux react-redux