File tree Expand file tree Collapse file tree 1 file changed +74
-0
lines changed Expand file tree Collapse file tree 1 file changed +74
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Enforce state initialization style (react/state-in-constructor)
2
+
3
+ This rule will enforce the state initialization style to be either in a constructor or with a class property.
4
+
5
+ ## Rule Options
6
+
7
+ ``` js
8
+ ...
9
+ " react/state-in-constructor" : [< enabled> , < mode> ]
10
+ ...
11
+ ```
12
+
13
+ ### ` always ` mode
14
+
15
+ Will enforce the state initialization style to be in a constructor. This is the default mode.
16
+
17
+ The following patterns are considered warnings:
18
+
19
+ ``` jsx
20
+ class Foo extends React .Component {
21
+ state = { bar: 0 }
22
+ render () {
23
+ return < div> Foo< / div>
24
+ }
25
+ }
26
+ ```
27
+
28
+ The following patterns are ** not** considered warnings:
29
+
30
+ ``` jsx
31
+ class Foo extends React .Component {
32
+ constructor (props ) {
33
+ super (props)
34
+ this .state = { bar: 0 }
35
+ }
36
+ render () {
37
+ return < div> Foo< / div>
38
+ }
39
+ }
40
+ ```
41
+
42
+ ### ` never ` mode
43
+
44
+ Will enforce the state initialization style to be with a class property.
45
+
46
+ The following patterns are considered warnings:
47
+
48
+ ``` jsx
49
+ class Foo extends React .Component {
50
+ constructor (props ) {
51
+ super (props)
52
+ this .state = { bar: 0 }
53
+ }
54
+ render () {
55
+ return < div> Foo< / div>
56
+ }
57
+ }
58
+ ```
59
+
60
+ The following patterns are ** not** considered warnings:
61
+
62
+ ``` jsx
63
+ class Foo extends React .Component {
64
+ state = { bar: 0 }
65
+ render () {
66
+ return < div> Foo< / div>
67
+ }
68
+ }
69
+ ```
70
+
71
+
72
+ ## When Not To Use It
73
+
74
+ When the way a component state is being initialized doesn't matter.
You can’t perform that action at this time.
0 commit comments