【发布时间】:2017-07-11 11:44:54
【问题描述】:
在我的应用程序中,有阶段和游戏对应一个阶段。我在componentDidMount中获取阶段,然后在componentWillReceiveProps中检查reducer中是否有stageId,然后为阶段获取游戏。用于获取游戏的动作舞台被无限次触发。有人可以解释为什么吗?
componentDidMount() {
this.props.fetchCurrentStage();
}
componentWillReceiveProps(nextState) {
if (nextState.stageReducer && nextState.stageReducer.stageId) {
this.props.fetchGamesForStage(nextState.stageReducer.stageId);// Corresponding action is triggered infinite times.Why?
}
}
【问题讨论】:
-
您是否尝试过
console.lognextStage.stageReducer和nextState.stageReducer.stageId的值?如果在无限循环中调用该操作,则每次更新状态并触发重新渲染时,该条件都必须为真。 -
现在,我不知道你的整个应用逻辑。但是当孩子被挂载时,您正在从父
fetchCurrentStage()调用一个函数。直接从父级获取它并将结果作为道具传递下来不是更好吗?这不会解决您的问题 - 只是我的观察。 -
另外,
nextState应该叫nextProps? -
你能分享你的
mapStateToProps函数吗? -
不要在 componentWillReceiveProps 中触发动作。这是不正确的。触发动作时更新道具。我确定该操作需要在其他地方调用。该操作将依次更新存储,并且此事件将再次触发,从而导致无限循环。
标签: reactjs redux react-redux redux-saga