This is a follow up question to this question which is the nearest to my issue:
Infinite loop in useEffect
I am creating a small React.js app to study the library. I'm getting this 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.
I got a functional component, in which there is this code:
const propertiesMap2 = new Map([ //There is also propertiesMap1 which has the same structure ["TITLE4", { propertyValues: { myProperty10 : "myVal1", myProperty11 : "myVal2", myProperty12 : "myVal3", }, isOpen: true } ], ["TITLE5", { propertyValues: { myProperty13 : "myVal4", myProperty14 : "myVal5", myProperty15 : "myVal6", }, isOpen: true } ], ["TITLE6", { propertyValues:{ myProperty16 : "myVal7", myProperty17 : "myVal8", myProperty18 : "myVal9", }, isOpen: true } ] ]); const [properties, setPropertiesMapFunc] = useState(new Map()); useEffect(()=> { let mapNum = Number(props.match.params.id); setPropertiesMapFunc(mapNum === 1 ?propertiesMap1 : propertiesMap2); }, [properties]);
The correct properties map is chosen each time, but like I said I get this error. Why do I get it, if the propertiesMap
is constant without anything changing, and properties
was passed as a parameter to setEffect
, so I thought it would only re render when something there changes..
I wrote my codes and i think everything is right but i get an error from react.js Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
I am creating a small React.js app to study the library. I'm getting this 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.
This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops. And here's the code . it's referencing.
React limits the number of nested updates to prevent infinite loops. And here's the code . it's referencing.
Because you are creating the map objects inside of your component function they will be recreated on every render. Because of that your effect will set a new map as the new state which will in turn trigger another re-render and your effect being called again which leads to an infinite update loop.
You can move the definition of your map objects outside of your component to fix this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With