warning: in some cases setState() may be called after the unmount
See original GitHub issueThis warning does happen from time to time:
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 ReactMapboxGl component.
The related code:
map.on('load', (evt: React.SyntheticEvent<any>) => {
this.setState({ map });
if (onStyleLoad) {
onStyleLoad(map, evt);
}
});
https://github.com/alex3165/react-mapbox-gl/blob/master/src/map.tsx#L181
If the ReactMapboxGl component is unmounted before load
finishes, the callback will try to update the state, it does nothing but this is not recommended
calling setState() in an unmounted component means that your app
is still holding a reference to the component after the component
has been unmounted - which often indicates a memory leak
https://facebook.github.io/react/blog/2015/12/16/ismounted-antipattern.html
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Prevent React setState on unmounted Component
Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application....
Read more >React warning about setState in unmounted component
Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application...
Read more >Avoid React state update warnings on unmounted components
React raising a warning when you try to perform a state update on an unmounted component. React setState is used to update the...
Read more >Avoid Memory Leak With React SetState On An Unmounted ...
Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application...
Read more >Avoid setState warnings on unmounted React components
Otherwise you'll get the following warning : " Warning : Can 't call setState (or forceUpdate) on an unmounted component. This is a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I got the same error. In my case, I used
this.setState({ key: value });
in the constructor of a component. It’s fixed by replaced withthis.state = { key: value };
, though this style seems not recommended by React.I also faced this issue sometimes. I’m not really convinced about passing the map as state is the right way to go. This brings to a potentially bigger issue: is it correct to use the React context API here? Wouldn’t it be better to user redux + props?
Of course this would mean redesign the map as HOC and have a Redux state and all the map children such as Layer, Source, whatever… as connected components.