0

我正在使用 React、Redux 和一些 antd 组件

我正在使用这个模式来提醒用户错误,但如果你看它,它不完全是一个组件,它是一个函数,所以现在我正在使用componentDidUpdate这样的:

componentDidUpdate () {
  if (this.props.states.ErrorReducer.displayError) {
    error(() => {
      this.props.dispatch(ErrorActionCreators.acceptError())
    }, this.props.states.ErrorReducer.errorMessage )
  }
}

问题是,如果我一次对状态进行多项更改,例如对 API 进行多次调用并且它们在不同时间更改状态,则此模式会多次打开。

我可以使用状态来做类似的事情

if (this.state.displayError !== this.props.displayError {
  updateState();
  error();
}

但我避免使用 React 状态。

无论如何我可以检查组件上的某个特定道具是否已更改?

4

1 回答 1

1

您可以使用生命周期方法 componentWillReceiveProps。每次更新道具时都会调用它。这是帮助componentWillreceiveProps的链接和代码片段:

componentWillReceiveProps(nextProps){
  if(this.props !== nextProps){
    // your code and conditions go here
  }
}

您实际上可以将旧道具 (this.props) 与新的或更新的道具 (nextProps) 进行比较。

于 2017-06-06T16:29:28.440 回答