Skip to content

Rule proposal: no-access-state-in-setstate#1374

Merged
ljharb merged 4 commits intojsx-eslint:masterfrom
jaaberg:no-access-state-in-setstate
Nov 3, 2017
Merged

Rule proposal: no-access-state-in-setstate#1374
ljharb merged 4 commits intojsx-eslint:masterfrom
jaaberg:no-access-state-in-setstate

Conversation

@jaaberg
Copy link
Copy Markdown
Contributor

@jaaberg jaaberg commented Aug 15, 2017

This rule prevents the use of this.state within this.setState. It is further described in docs/rules/no-access-state-in-setstate.md in the PR, but to summarize: this.setState is async, and referring to this.state within it may lead to race conditions.

This is my first eslint-plugin PR, so I am really happy to get any kind of feedback.

edit:
This is also the recommended way doing it in the react docs:

If the next state depends on the previous state, we recommend using the updater function form, instead

this.setState((prevState) => {
 return {counter: prevState.quantity + 1};
}); 

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

7 participants