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

问题描述 投票:0回答:2

我有以下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

reactjs react-native react-redux
2个回答
3
投票

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

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


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
})
© www.soinside.com 2019 - 2024. All rights reserved.