New rule: react-set-state-with-function
See original GitHub issueEnforce usage of the callback form of this.setState
// bad
this.setState({ ... });
// good
this.setState((prevState, props) => ...);
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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?
It seems like disallowing
this.setState({...});
might be a little too restrictive. It seems like a better approach might be #172