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.

Why no setState in componentDidMount?

See original GitHub issue

This is less of an issue and more of a question on how you handle things.

React docs says to put ajax-requests in componentDidMount. But I would also like to cancel the request in componentWillUnmount as that data is not needed if the component isn’t there. And to do so, I need a reference to the promise created in componentDidMount. I thought to achieve this by putting it on state, like so:

componentDidMount() {
  this.setState({ serverRequest: this.props.fetcData() });
}

componentWillUnmount() {
  const { serverRequest } = this.state;

  if (serverRequest.isPending()) serverRequest.cancel();
}

(This is using bluebird btw, not native promises)

And this works, but the linter complains about the setState-call in componentDidMount.

I tracked down the issue where the rule was added (#345 & #346), and while the OP states that it follows your style guide, I can’t find any reference to that rule in your docs.

So, my question is: Do you have any other way of keeping around references to async stuff created in componentDidMount such as ajax-requests, setTimeouts etc to be able to clean them up in componentWillUnmount? Or should I just disable the rule for that particular line?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:42
  • Comments:35 (5 by maintainers)

github_iconTop GitHub Comments

238reactions
ljharbcommented, Jan 17, 2016

Why not put the setState call in componentWillMount? That would be the inverse of componentWillUnmount

95reactions
SimenBcommented, Jan 17, 2016

As the docs state that they belong in componentDidMount:

If you want to integrate with other JavaScript frameworks, set timers using setTimeout or setInterval, or send AJAX requests, perform those operations in this method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting state on componentDidMount() - reactjs - Stack Overflow
According to the React Documentation it's perfectly OK to call setState() from within the componentDidMount() function ...
Read more >
React.Component
You may call setState() immediately in componentDidMount() . It will trigger an extra rendering, but it will happen before the browser updates the...
Read more >
Using setState in React components - LogRocket Blog
componentDidMount() invokes immediately after a component mounts. You can call setState() immediately in componentDidMount() and triggers an ...
Read more >
Prevent reducing your React application performance with a ...
This error will be something like “ESLint: Do not use setState in componentDidMount (react/no-did-mount-set-state” which warns you not to ...
Read more >
Understanding React componentDidMount and how it works
The answer is yes! Because componentDidMount() only triggers once, I don't see any issues, nor have I experienced any problems by making that...
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