forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add doc for state-in-constructor rule
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Enforce state initialization style (react/state-in-constructor) | ||
|
||
This rule will enforce the state initialization style to be either in a constructor or with a class property. | ||
|
||
## Rule Options | ||
|
||
```js | ||
... | ||
"react/state-in-constructor": [<enabled>, <mode>] | ||
... | ||
``` | ||
|
||
### `always` mode | ||
|
||
Will enforce the state initialization style to be in a constructor. This is the default mode. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
state = { bar: 0 } | ||
render() { | ||
return <div>Foo</div> | ||
} | ||
} | ||
``` | ||
|
||
The following patterns are **not** considered warnings: | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
constructor(props) { | ||
super(props) | ||
this.state = { bar: 0 } | ||
} | ||
render() { | ||
return <div>Foo</div> | ||
} | ||
} | ||
``` | ||
|
||
### `never` mode | ||
|
||
Will enforce the state initialization style to be with a class property. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
constructor(props) { | ||
super(props) | ||
this.state = { bar: 0 } | ||
} | ||
render() { | ||
return <div>Foo</div> | ||
} | ||
} | ||
``` | ||
|
||
The following patterns are **not** considered warnings: | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
state = { bar: 0 } | ||
render() { | ||
return <div>Foo</div> | ||
} | ||
} | ||
``` | ||
|
||
|
||
## When Not To Use It | ||
|
||
When the way a component state is being initialized doesn't matter. |