File tree 1 file changed +4
-4
lines changed
1 file changed +4
-4
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ Such usage of `this.state` might result in errors when two state calls are
5
5
called in batch and thus referencing old state and not the current
6
6
state. An example can be an increment function:
7
7
8
- ```
8
+ ``` javascript
9
9
function increment () {
10
10
this .setState ({value: this .state .value + 1 });
11
11
}
@@ -14,15 +14,15 @@ function increment() {
14
14
If these two ` setState ` operations is grouped together in a batch it will
15
15
look be something like the following, given that value is 1:
16
16
17
- ```
17
+ ``` javascript
18
18
setState ({value: 1 + 1 })
19
19
setState ({value: 1 + 1 })
20
20
```
21
21
22
22
This can be avoided with using callbacks which takes the previous state
23
23
as first argument:
24
24
25
- ```
25
+ ``` javascript
26
26
function increment () {
27
27
this .setState (prevState => ({value: prevState .value + 1 }));
28
28
}
@@ -33,7 +33,7 @@ even when things happen in batches. And the example above will be
33
33
something like:
34
34
35
35
36
- ```
36
+ ``` javascript
37
37
setState ({value: 1 + 1 })
38
38
setState ({value: 2 + 1 })
39
39
```
You can’t perform that action at this time.
0 commit comments