有没有办法知道在不使用反应状态的情况下更新了特定的道具



我正在使用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();
}

,但我避免使用反应状态。

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

您可以使用生命周期方法,componentWillReceiveProps。每次提供道具时都会调用。这是一个帮助componentWillReceiveProps和代码段的链接:

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

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

相关内容

最新更新