回答編集履歴
3
質問上のコードとReact公式サイト上のコードをあわせました
answer
CHANGED
@@ -17,7 +17,7 @@
|
|
17
17
|
```
|
18
18
|
handleClick = (e) => {
|
19
19
|
this.setState((prevState, props) => ({
|
20
|
-
count: prevState.
|
20
|
+
count: prevState.count + 1
|
21
21
|
});
|
22
22
|
};
|
23
23
|
```
|
2
リンク先を参照ページの該当箇所アンカーに変更
answer
CHANGED
@@ -3,8 +3,8 @@
|
|
3
3
|
# 参考
|
4
4
|
> Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state.
|
5
5
|
|
6
|
-
[https://reactjs.org/docs/state-and-lifecycle.html
|
6
|
+
[https://reactjs.org/docs/state-and-lifecycle.html#state-updates-may-be-asynchronous
|
7
|
-
](https://reactjs.org/docs/state-and-lifecycle.html)
|
7
|
+
](https://reactjs.org/docs/state-and-lifecycle.html#state-updates-may-be-asynchronous)
|
8
8
|
|
9
9
|
# Wrong
|
10
10
|
```
|
1
Because this.props and this.state may be updated asynchronously, you should not rely on their values
answer
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
質問の主題からは外れますが、`this.setState({ count: this.state.count + 1 })`の部分は公式サイトでも注意喚起されている好ましくない書き方です。`this.setState`は非同期で実行されるため、以下のようにthis.stateの値が本当に今現在のstateなのか保証されません。
|
2
2
|
|
3
3
|
# 参考
|
4
|
+
> Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state.
|
5
|
+
|
4
6
|
[https://reactjs.org/docs/state-and-lifecycle.html
|
5
7
|
](https://reactjs.org/docs/state-and-lifecycle.html)
|
8
|
+
|
6
9
|
# Wrong
|
7
10
|
```
|
8
11
|
handleClick = (e) => {
|