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.

New rule: react-set-state-with-function

See original GitHub issue

Enforce usage of the callback form of this.setState

// bad
this.setState({ ... });
// good
this.setState((prevState, props) => ...);

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
vhfmagcommented, May 5, 2017

I don’t know much about tslint and tsutils API, so that may be not feasible, but I think the ideal would be that the rule allows objects if props and state aren’t used and enforce callback usage otherwise. Does that sound feasible?

1reaction
benbankescommented, Aug 10, 2018

It seems like disallowing this.setState({...}); might be a little too restrictive. It seems like a better approach might be #172

Read more comments on GitHub >

github_iconTop Results From Across the Web

State and Lifecycle - React
This page introduces the concept of state and lifecycle in a React component. You can find a detailed component API reference here.
Read more >
How to become a pro with React setState() in 10 minutes
You should always do this kind of manipulation with a functional approach, supplying the state and props and returning the new state based ......
Read more >
React setState: setstate Objects and Callback Functions
Three Rules of Thumb When Using setState( ) ... Never change a state variable directly, always use a function to change a state's...
Read more >
Can I execute a function after setState is finished updating?
I just created a new object which is required by the function to run after the state updates and and simply used it....
Read more >
Understanding React `setState` | CSS-Tricks
When the request to setState() is triggered, React creates a new tree containing the reactive elements in the component (along with the updated ......
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