尽管返回了新的状态对象,但未调用componentWillReceiveProps

时间:2017-12-27 22:54:39

标签: reactjs react-native react-redux

我有以下mapDispatchToProps

const mapDispatchToProps = dispatch => ({
  surveysRequest: () => dispatch(DashboardActions.surveysRequest()),
  surveysFilter: () => dispatch(DashboardActions.surveysFilter())
});

并在componentDidMount我调用方法

componentDidMount() {
  if (this.animation) {
    this.animation.play();
  }

  this.props.surveysRequest()
}

surveysSuccess减速器确实被调用,即使我返回一个新状态,componentWillReceiveProps也从未被调用

const surveysSuccess = createAsyncSuccess(ASYNC_GET, (state, action) => {
    return state.merge({
      surveys: action.data
    })
  }
)

使用最新的点火反应原生样板https://github.com/infinitered/ignite

2 个答案:

答案 0 :(得分:3)

https://levelup.gitconnected.com/componentdidmakesense-react-lifecycle-explanation-393dcb19e459

componentWillReceiveProps仅在组件收到的道具发生变化时调用。如果状态发生变化,则不会调用componentWillReceiveProps。尝试使用componentWillUpdatecomponentShouldUpdate

答案 1 :(得分:0)

您正在对状态对象进行浅层复制,并且仍然通过引用传递嵌套值。

From docs here

  只要连接组件接收到由浅等式比较确定的新道具,

mapStateToProps 函数也将被重新调用。

解决方案:

const surveysSuccess = createAsyncSuccess(ASYNC_GET, (state, action) => {

    let newState= JSON.parse(JSON.stringify(state)); //add it before using state.
     //rest code
})