You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
NEVER mutate this.state directly, as calling setState() afterwards may replace
the mutation you made. Treat this.state as if it were immutable.
The only place that's acceptable to assign this.state is in a ES6 class component constructor.
Rule Details
This rule is aimed to forbid the use of mutating this.state directly.
Examples of incorrect code for this rule:
varHello=createReactClass({componentDidMount: function(){this.state.name=this.props.name.toUpperCase();},render: function(){return<div>Hello {this.state.name}</div>;}});classHelloextendsReact.Component{constructor(props){super(props)// Assign at instance creation time, not on a callbackdoSomethingAsync(()=>{this.state='bad';});}}
Hi, I just ran into this error while working on a non-react project.
Unfortunately this rule applies to anything mutating something that looks like this.state, even when it's not in a react context at all. In my case it's in a choo context, where mutating state is conversely the norm and expected, and there is no setState method.
What this means is this rule is unilaterally enforcing a react convention for all code that looks similar, even when it shouldn't apply at all. I'm pegging standard to 15 for now -- I don't want to have to write eslint-disable-rule in possibly hundreds of component files.
Hoping there's a reasonable compromise here -- alternately we can eject and modify standard eslint rules ourselves, but I'd prefer to remain on the good ship standard.
https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-direct-mutation-state.md
NEVER mutate
this.state
directly, as callingsetState()
afterwards may replacethe mutation you made. Treat
this.state
as if it were immutable.The only place that's acceptable to assign this.state is in a ES6
class
component constructor.Rule Details
This rule is aimed to forbid the use of mutating
this.state
directly.Examples of incorrect code for this rule:
Examples of correct code for this rule:
The text was updated successfully, but these errors were encountered: