Home > Enterprise >  How to solve this warning
How to solve this warning

Time:05-01

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

CodePudding user response:

useEffect(() => {
    let isMounted = true
    isMounted && "Do Your Thing Here"
    return () => { isMounted = false }
}, [])

CodePudding user response:

I believe the first useEffect (the one that calls function, you want) should have an empty dependency array. Without it the effect is continually running since function changes state every time it runs.

CodePudding user response:

Just add an empty dependency array inside useEffect's second argument, like this :

const [state,setState] = useState();

useEffect(()=>{
   ... other tasks or/and update state 
},[]);
  • Related