The warning:
Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the _class component.
Any idea where it can come from. Gotchas or things like that ?
I already looked at all the setState in my code and replaced them to make sure. Can't find where it comes from...
My observations so far:
So ! I understand what the error is but this time the warning is about a _class component
so I'm lost... I just upgraded to react-router
v4 and it needed a lot of changes so it's hard to localize the source of the warning.
Anyone had had a similar problem before ?
EDIT:
I found the setState
that were causing problem. It was in react-router-server
. I'll look into it to see if I can fix it !
Thanks @zerkms for the idea to look with a debugger to get the line number since there was no trace in the terminal.
I used the v8 experimental inspector(https://stackoverflow.com/a/39901169/3687661). Works pretty good :)
Summary. Seeing called setState() on an unmounted component in your browser console means the callback for an async operation is still running after a component's removed from the DOM. This points to a memory leak caused by doing redundant work which the user will never benefit from.
Do not call setState() here because the component does not rerender. Once a component instance unmounts, it never mounts again.
The warning "Can't perform a React state update on an unmounted component" is caused when we try to update the state of an unmounted component. A straight forward way to get rid of the warning is to keep track of whether the component is mounted using an isMounted boolean in our useEffect hook.
This error usually happens when you call the React setState method in a Class component's constructor. The reason is that the component state is not yet initialized when the constructor is called.
This is common due to async activities like API calls. For example, this happen when you try to set a state after you receive the data from the server and the corresponding page to receive that state is not mounted.
To avoid this, check if the component is mounted before setting state in that component. Use a flag to check, say this.mounted = true
in componentDidMount
and change the flag to false in componentWillUnmount
. Use this.mounted
across the component to check if the component is mounted. This will fix the warning.
Hope this helps!
This usually happens when you call this.setState
within a setTimeout
or setInterval
or some other deferred function.
If you are using setTimeout
/setInterval
make sure you call clearTimeout
/clearInterval
in componentWillUnmount
.
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