[英]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.