question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

warning: in some cases setState() may be called after the unmount

See original GitHub issue

This 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
TLiu2014commented, Sep 21, 2017

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 with this.state = { key: value };, though this style seems not recommended by React.

1reaction
lamuertepeludacommented, Apr 10, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found