【问题标题】:componentDidUpdate() infinite loopcomponentDidUpdate() 无限循环
【发布时间】:2021-08-26 17:08:37
【问题描述】:

当我尝试设置状态时收到一条关于 componentDidUpdate 的错误消息,但我不知道为什么。

componentDidUpdate() {
  if(this.props.isLoadingDept===false &&  this.props.isLoadingEmpStatus===false
  && this.props.isLoadingGroup===false &&  this.props.isLoadingEmpDesig===false){
        this.setState({
    button:false
  })
  console.log('UPDATE')
}
}

当语句为真时,控制台日志值只记录一次,所以我不确定它是如何以及为什么循环的..

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    您可以添加另一个条件来检查状态中的按钮是否已经为假,以防止无限循环。

    if(this.props.isLoadingDept===false &&  this.props.isLoadingEmpStatus===false
      && this.props.isLoadingGroup===false &&  this.props.isLoadingEmpDesig===false && this.state.button !== false)
    

    因为一旦你更新了状态,componentDidUpdate 就会被调用并且你在这个条件下使用的道具没有改变。

    【讨论】:

      猜你喜欢
      • 2021-07-12
      • 2016-10-22
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多