简体   繁体   English

超过最大更新深度。 当组件在 useEffect 中调用 setState 时,可能会发生这种情况,

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

相关问题 错误“超出最大更新深度。当组件在 useEffect 中调用 setState 时可能会发生这种情况” - Error "Maximum update depth exceeded. This can happen when a component calls setState inside useEffect" 错误:超过最大更新深度。 当组件在 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 时,可能会发生这种情况 - Error: Max update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate 超出最大更新深度。 反应使用效果 - Maximum update depth exceeded. React useEffect 超过最大更新深度。 没有 useState 或 useEffect - Maximum update depth exceeded. No useState or useEffect 错误:超过最大更新深度。 setState 抛出错误 - Error: Maximum update depth exceeded. setState throws error 反应 useEffect 警告:超出最大更新深度。 有没有解决的办法? - React useEffect Warning: Maximum update depth exceeded. Is there a way around this? React Refs woth setState给出超出最大更新深度。 - React Refs woth setState giving Maximum update depth exceeded. 不变违反:最大更新深度..当组件重复调用componentWillUpdate或componentDidUpdate内部的setState时 - Invariant Violation: Maximum update depth..when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM