material-components-web-react: Can't perform a React state update on an unmounted component
Seeing this error when I hide a div that contains many ChipSet components.
index.js:1452 Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
in RippledComponent
in div
in ChipSet
It seems to happen right before execution of this line:
I see there is no “mounted” check prior to this setState call, maybe this could be the source of error.
About this issue
- Original URL
- State: open
- Created 6 years ago
- Comments: 17 (2 by maintainers)
Fix it with this technique.
Look where I added
_isMountedand follow the same in you code@mohokh67 What about solution for a functional component?
For functional components you can use a custom hook:
and then use it in your component:
PS: This might not be worth while.
@5ervant you can use useRef for check if the component still mounted
example
Try https://github.com/iShawnWang/withUnmounted package as an elegant solution
I have met this problem in a situation where the keys were massed up. So it might be coming from above the component itself. In my case the mistake was caused by how I have been generating the keys:
That causes a memory leak as React can’t notice some elements must be removed or created as the key changes on any other change in the collection element. My conclusion is that key should be unique during the life cycle so the state mechanism can rely on it.
look at it: https://www.robinwieruch.de/react-warning-cant-call-setstate-on-an-unmounted-component