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.

handleResize should not call setState when unmounted

See original GitHub issue

I’m getting this console warning:

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 %s.%sthe componentWillUnmount method
    in ImageGallery (created by Context.Consumer)

Tracked it down to the handleResize method calling setState.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
pa-ulandercommented, Apr 21, 2020

Hi, just to confirm, I have encountered the same issue. Cheers!

1reaction
Denis2222commented, Mar 29, 2021

This bug appears when the gallery has just loaded and is unmounted

To reproduce it navigate to a page with gallery, and quickly push back button.

  • /Homepage
  • Click -> /User/9999 ( view with react-image-gallery component)
  • ImageGallery:contruct
  • ImageGallery:componentDidMount
  • ImageGallery:initResizeObserver
  • Quickly Click Back Button -> /Homepage
  • ImageGallery:componentWillUnmount
  • Homepage start render
  • ImageGallery:handleResize ( From :initResizeObserver(element) {…} ) 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.

Not easy to reproduce but I am using your gallery for a school project. No error tolerated in the console.

Easy fix

componentWillUnmount() {
   this.setState = () => {}
   ...
}

In any case, well done for this component. Almost perfect ! Best react gallery.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent React setState on unmounted Component
It usually means you have called setState on an unmounted component . ... not calling setState on unmounted components should show up too....
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 >
Can't call setState on an unmounted component
As a note, don't call async/await functions in the constructor. The constructor should initialize the component and return it as soon as ...
Read more >
Fixing React's “Called SetState() on an ... - How-To Geek
Seeing Called setState() on an Unmounted Component in your console is one of the most frequent issues faced by newcomers to React.
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 >

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