[英]Maximum update depth exceeded. This can happen when a component calls setState inside useEffect,
useEffect(() => {
const onChildAdd = database().ref('/User/' + user.uid ).on('value', snapshot => {
setComplete(snapshot.val().Complete);
setUserProfile(snapshot.val().User);
setStartYear(snapshot.val().StartYear);
setDisplayName(snapshot.val().displayName);
setDisplayPicture(snapshot.val().photoURL);
// ...
});
return () =>
database().ref('/User/' + user.uid )
.off('value', onChildAdd);
}, [user.uid])
This is how each of them is used这就是他们每个人的使用方式
<Text>My name is {displayName}</Text>
Error Warning: Maximum update depth exceeded.错误警告:超过最大更新深度。 This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.当组件在 useEffect 中调用 setState 时,可能会发生这种情况,但 useEffect 要么没有依赖数组,要么每次渲染时依赖项之一发生变化。
Are you using default value in code above this snippet?您是否在此代码段上方的代码中使用默认值?
if you do如果你这样做
const myComponent (props)=>{
const {user = {}, stuff } = props;
{/* code here that accesses 'user' object inside hooks */}
return <h1>Hi></h1>
}
A new object may be created for the 'user' variable on every render!可以在每次渲染时为“用户”变量创建一个新的 object! Either use defaultprops or move the empty object creation out of the render loop使用 defaultprops 或将空的 object 创建移出渲染循环
const empty = {};
const myComponent (props)=>{
const {user = empty, stuff } = props;
{/* code here that accesses 'user' object inside hooks */}
return <h1>Hi></h1>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.