no-access-state-in-setstate
Configuration
rslint.config.ts
Rule Details
Disallow reading this.state inside the first argument of this.setState(...).
Because React schedules and batches state updates asynchronously, the value
of this.state at the moment setState is called is not necessarily the
value the updater will see when it runs. Writing an update that derives from
the previous state via direct this.state access can therefore yield stale
results. The correct form passes a callback — this.setState(prev => ...) —
which always receives the latest committed state.
Examples of incorrect code for this rule:
Examples of correct code for this rule: