【问题标题】:Error: Maximum update depth exceeded. React Native错误:超过最大更新深度。反应原生
【发布时间】:2021-09-06 18:56:19
【问题描述】:

我尝试在 compenetDidUpdate 中设置状态,但它显示错误无限循环。有什么解决办法吗?最初我将 setState 放在一个函数中,但也面临这个错误。我正在使用类组件代码

 componentDidUpdate(){
          if(isEmpty(this.props.AESDetail) == false){
            if(this.props.AESDetail.length != 0){
              if(this.props.APIESDetail.length != 0){
                  if(this.props.APIESDetail.Focus != null){
                      this.setState({
                          gotFocusApies: true
                      })
                  }
              }
            }
        }   
      }

错误:超过最大更新深度。当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,可能会发生这种情况。 React 限制了嵌套更新的数量以防止无限循环。

【问题讨论】:

    标签: react-native


    【解决方案1】:

    看起来您可能只需要进行一项额外测试,以确保 gotFocusApies 还不是 true

    这些 if 语句也可能更好地合并为一个。 注意:list.length != 0 通常可以替换为 list.length!!list.length

    componentDidUpdate() {
      if (
        isEmpty(this.props.AESDetail) == false &&
        this.props.AESDetail.length &&
        this.props.APIESDetail.length &&
        this.props.APIESDetail.Focus != null &&
        !this.state.gotFocusApies
      ) {
        this.setState({ gotFocusApies: true });
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-15
      • 2021-06-20
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 2021-07-12
      • 2019-02-14
      • 2019-07-17
      • 1970-01-01
      相关资源
      最近更新 更多