-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docs for no-direct-mutation-state
- Loading branch information
1 parent
329c4c8
commit cf6e8fc
Showing
4 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Prevent direct mutation of this.state (no-direct-mutation-state) | ||
|
||
NEVER mutate `this.state` directly, as calling `setState()` afterwards may replace | ||
the mutation you made. Treat `this.state` as if it were immutable. | ||
|
||
## Rule Details | ||
|
||
This rule is aimed to forbid the use of mutating `this.state` directly. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
var Hello = React.createClass({ | ||
componentDidMount: function() { | ||
this.state.name = this.props.name.toUpperCase(); | ||
}, | ||
render: function() { | ||
return <div>Hello {this.state.name}</div>; | ||
} | ||
}); | ||
``` | ||
|
||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
var Hello = React.createClass({ | ||
componentDidMount: function() { | ||
this.setState({ | ||
name: this.props.name.toUpperCase(); | ||
}); | ||
}, | ||
render: function() { | ||
return <div>Hello {this.state.name}</div>; | ||
} | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters