Logo Awesome React

Awesome React

Avoid setState warnings on unmounted React components

  • Details
Codesandbox: https://codesandbox.io/s/nwwrrpvqw4

If you're doing any async stuff in your React component and the callback calls this.setState, you need to either cancel the async stuff or take care to not call setState if the component is unmounted. Otherwise you'll get the following warning:

"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. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method."

This is how you fix this. We have examples with setInterval as well as axios.