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.

Document the use of setState in componentWillUnmount

See original GitHub issue

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

Excerpt from documentation:

componentWillUnmount() is invoked immediately before a component is unmounted and destroyed. Perform any necessary cleanup in this method, such as invalidating timers, canceling network requests, or cleaning up any subscriptions that were created in componentDidMount().

https://reactjs.org/docs/react-component.html

The docs don’t mention if setState can be used in componentWillUnmount.

Consider the following example where the state.showGreeting is undone by timer over time. But since the timer has to be invalidated in componentWillUnmount there no other place to reset the state:

class HelloWorld extends Component {
  state = {
    showGreeting: false
  };

  onClick() {
    this.setState({ showGreeting: true })

    this._timer = setTimeout(() => this.setState({ showGreeting: false }), 3000)
  }

  componentWillUnmount() {
    clearTimeout(this._timer)

    // is it legal?
    this.setState({ showGreeting: false })
  }
}

Is it legal to call setState from componentWillUnmount? Given that it can be asynchronous it feels that setState may not be invoked until after component is actually unmounted which may produce a warning in my understanding, until… componentWillUnmount actually pumps up the state’s dispatch queue manually to ensure that all state changes land in component before it’s too late.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

What is the expected behavior?

Not sure

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

16.2 / Webkit

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

15reactions
gaearoncommented, Jan 29, 2018

No, it’s not supported or useful. (Happy to take a PR documenting that)

I’m not sure what you’re trying to achieve. If the component is being unmounted, there’s no need to “reset” the state. A component can’t be “resurrected” so if it gets unmounted, its state is irrelevant to the rest of the program.

12reactions
gaearoncommented, Jan 29, 2018

Maybe we could add a notice in componentWillUnmount doc that once a component is unmounted, it can never be re-mounted.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how can I setState in unmount Component in React
I would suggest to use react-router component method instead of render and ... componentDidMount(){ var {state, setState} = this.props; ...
Read more >
Prevent React setState on unmounted Component
To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method. In general, warnings don't crash your application.
Read more >
Fixing React's “Called SetState() on an ... - How-To Geek
Seeing called setState() on an unmounted component in your browser console means the callback for an async operation is still running after a ......
Read more >
ReactJS componentWillUnmount() Method - GeeksforGeeks
The componentWillUnmount() method allows us to execute the React code when the component gets destroyed or unmounted from the DOM (Document ...
Read more >
Why should not call setState in componentWillUnmount? - JS IQ
You should not call setState() in componentWillUnmount() because once a component instance is unmounted, it will never be mounted again.
Read more >

github_iconTop Related Medium Post

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