简体   繁体   English

如何修复“超出最大更新深度。在 componentWillUpdate 或 componentDidUpdate 中调用 setState。” 反应错误?

[英]How to fix “Maximum update depth exceeded.calls setState inside componentWillUpdate or componentDidUpdate.” error in react?

The code was working fine but this came out of nowhere.代码工作正常,但这不知从何而来。

I tried switching the condition over to componentDidMount but that didn't work either.我尝试将条件切换到componentDidMount但这也不起作用。 Getting the error on this function.得到这个 function 的错误。

  componentDidUpdate() {
    if (
      this.props.firebase.auth.currentUser &&
      this.props.firebase.auth.currentUser.userProfile &&
      !this.state.userProfile
    ) {
      this.setState(
        {
          userProfile: this.props.firebase.auth.currentUser.userProfile
            .userProfile
        }
      );
    }
  }

It is supposed to be redirecting the user to the nextPage but gives me an error of "Maximum update depth exceeded".它应该将用户重定向到下一个页面,但给我一个“超出最大更新深度”的错误。

Why do you have.userProfile.userProfile?为什么你有.userProfile.userProfile? You're likely updating userProfile with undefined, so it's failing your .this.state.userProfile check.您可能会使用未定义的方式更新 userProfile,因此您的.this.state.userProfile检查失败。

I would remove the second.userProfile.我会删除 second.userProfile。

You're not passing prevProps to the componentDidUpdate and then using that within your conditional.您没有将prevProps传递给componentDidUpdate ,然后在条件中使用它。 Without more info about why you're using userProfile, I'd suggest doing something like below:如果没有关于您为什么使用 userProfile 的更多信息,我建议您执行以下操作:

componentDidUpdate(prevProps){
   if(this.props.firebase.auth.currentUser === this.prevProps.firebase.auth.currentUser) {
   ///your code
}}

Your logic for the this.props.firebase.auth.currentUser.userProfile &&.this.state.userProfile seems like it should not be located in this method, as it seems redundant if you are comparing whether the currentUser is still there and the same after the component updated.您对this.props.firebase.auth.currentUser.userProfile &&.this.state.userProfile的逻辑似乎不应该位于此方法中,因为如果您正在比较currentUser是否仍然存在并且相同,这似乎是多余的组件更新后。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:超过最大更新深度。 当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,可能会发生这种情况 - Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate 超出最大更新深度。 当组件在componentWillUpdate或componentDidUpdate中重复调用setState时,可能会发生这种情况 - Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate 不变违反:最大更新深度..当组件重复调用componentWillUpdate或componentDidUpdate内部的setState时 - Invariant Violation: Maximum update depth..when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate 在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState。 React 限制嵌套更新的数量以防止无限循环 - repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops ComponentDidUpdate - 超出最大更新深度 - 错误 - ComponentDidUpdate - Maximum update depth exceeded - Error componentDidUpdate - 错误:超出最大更新深度 - componentDidUpdate - Error: Maximum update depth exceeded 错误“超出最大更新深度。当组件在 useEffect 中调用 setState 时可能会发生这种情况” - Error "Maximum update depth exceeded. This can happen when a component calls setState inside useEffect" 在 componentDidUpdare 中反应 setState 导致超过最大更新深度 - React setState inside componentDidUpdare leads to Maximum update depth exceeded 尝试使用react组件中的return内部的setstate更新状态并获得“最大更新深度超出错误”? - Trying to update the state using setstate inside the return in react component and getting “Maximum update depth exceeded error”? setState() 不工作,错误:超过最大更新深度 - setState() not working, Error: Maximum update depth exceeded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM