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
Copy file name to clipboardExpand all lines: docs/rules/no-did-mount-set-state.md
+5-7Lines changed: 5 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ Updating the state after a component mount will trigger a second `render()` call
4
4
5
5
## Rule Details
6
6
7
-
This rule is aimed to forbid the use of `this.setState` in `componentDidMount`.
7
+
This rule is aimed to forbid the use of `this.setState` in `componentDidMount` outside of functions, such as callbacks.
8
8
9
9
The following patterns are considered warnings:
10
10
@@ -21,6 +21,8 @@ var Hello = React.createClass({
21
21
});
22
22
```
23
23
24
+
The following patterns are not considered warnings:
25
+
24
26
```js
25
27
var Hello =React.createClass({
26
28
componentDidMount:function() {
@@ -36,8 +38,6 @@ var Hello = React.createClass({
36
38
});
37
39
```
38
40
39
-
The following patterns are not considered warnings:
40
-
41
41
```js
42
42
var Hello =React.createClass({
43
43
componentDidMount:function() {
@@ -57,9 +57,9 @@ var Hello = React.createClass({
57
57
...
58
58
```
59
59
60
-
### `allow-in-func` mode
60
+
### `disallow-in-func` mode
61
61
62
-
By default this rule forbids any call to `this.setState` in `componentDidMount`. But since `componentDidMount` is a common place to set some event listeners, you may end up with calls to `this.setState` in some callbacks. The `allow-in-func` mode allows you to use `this.setState` in `componentDidMount` as long as they are called within a function.
62
+
By default this rule forbids any call to `this.setState` in `componentDidMount` outside of functions. The `disallow-in-func` mode makes this rule more strict by disallowing calls to `this.setState` even within functions.
63
63
64
64
The following patterns are considered warnings:
65
65
@@ -76,8 +76,6 @@ var Hello = React.createClass({
76
76
});
77
77
```
78
78
79
-
The following patterns are not considered warnings:
Copy file name to clipboardExpand all lines: docs/rules/no-did-update-set-state.md
+16-3Lines changed: 16 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,6 +32,21 @@ var Hello = React.createClass({
32
32
});
33
33
```
34
34
35
+
```js
36
+
var Hello =React.createClass({
37
+
componentDidUpdate:function() {
38
+
this.onUpdate(functioncallback(newName) {
39
+
this.setState({
40
+
name: newName
41
+
});
42
+
});
43
+
},
44
+
render:function() {
45
+
return<div>Hello {this.props.name}</div>;
46
+
}
47
+
});
48
+
```
49
+
35
50
## Rule Options
36
51
37
52
```js
@@ -42,7 +57,7 @@ var Hello = React.createClass({
42
57
43
58
### `allow-in-func` mode
44
59
45
-
By default this rule forbids any call to `this.setState` in `componentDidUpdate`. But in certain cases you may need to perform asynchronous calls in `componentDidUpdate` that may end up with calls to `this.setState`. The `allow-in-func` mode allows you to use `this.setState` in `componentDidUpdate` as long as they are called within a function.
60
+
By default this rule forbids any call to `this.setState` in `componentDidUpdate` outside of functions. The `disallow-in-func` mode makes this rule more strict by disallowing calls to `this.setState` even within functions.
46
61
47
62
The following patterns are considered warnings:
48
63
@@ -59,8 +74,6 @@ var Hello = React.createClass({
59
74
});
60
75
```
61
76
62
-
The following patterns are not considered warnings:
0 commit comments