no-did-mount-set-state
Configuration
Disallow this.setState inside componentDidMount.
Updating the state immediately after the initial mount triggers a second
render() call and can lead to property/layout thrashing.
Rule Details
This rule flags any this.setState call whose enclosing class method, class
field initializer, or object-literal property is keyed componentDidMount.
By default, calls inside a nested function (regular or arrow) are allowed —
enable disallow-in-func to forbid them too.
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Rule Options
With disallow-in-func set, the rule also flags this.setState calls inside
nested functions:
React Version
The rule is a no-op when settings.react.version is explicitly set to
>= 16.3.0, matching eslint-plugin-react's shouldBeNoop gate for
componentDidMount.
Differences from ESLint
- Pre-release version strings such as
"16.3.0-rc.1"are treated as16.3.0(rule becomes a no-op).eslint-plugin-reactfollows semver, where16.3.0-rc.1ranks below16.3.0, so it keeps the rule active. If you pin a pre-release version and want the rule to flagthis.setStateinsidecomponentDidMount, setsettings.react.versionto a release version such as"16.2.0".