質問するログイン新規登録

回答編集履歴

10

a

2018/07/05 03:57

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -65,9 +65,9 @@
65
65
 
66
66
  # 参考
67
67
 
68
- > Think of setState() as a request rather than an immediate command to update the component. For better perceived performance, React may delay it, and then update several components in a single pass. React does not guarantee that the state changes are applied immediately.
68
+ > **Think of setState() as a request rather than an immediate command to update the component. For better perceived performance, React may delay it, and then update several components in a single pass. React does not guarantee that the state changes are applied immediately.**
69
69
 
70
- > setState() does not always immediately update the component. It may batch or defer the update until later. This makes reading this.state right after calling setState() a potential pitfall. Instead, use componentDidUpdate or a setState callback (setState(updater, callback)), either of which are guaranteed to fire after the update has been applied. If you need to set the state based on the previous state, read about the updater argument below.
70
+ > **setState() does not always immediately update the component. It may batch or defer the update until later.** This makes reading this.state right after calling setState() a potential pitfall. Instead, use componentDidUpdate or a setState callback (setState(updater, callback)), either of which are guaranteed to fire after the update has been applied. If you need to set the state based on the previous state, read about the updater argument below.
71
71
 
72
72
  [https://reactjs.org/docs/react-component.html#setstate](https://reactjs.org/docs/react-component.html#setstate)
73
73
 

9

a

2018/07/05 03:57

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -69,4 +69,11 @@
69
69
 
70
70
  > setState() does not always immediately update the component. It may batch or defer the update until later. This makes reading this.state right after calling setState() a potential pitfall. Instead, use componentDidUpdate or a setState callback (setState(updater, callback)), either of which are guaranteed to fire after the update has been applied. If you need to set the state based on the previous state, read about the updater argument below.
71
71
 
72
- [https://reactjs.org/docs/react-component.html#setstate](https://reactjs.org/docs/react-component.html#setstate)
72
+ [https://reactjs.org/docs/react-component.html#setstate](https://reactjs.org/docs/react-component.html#setstate)
73
+
74
+ # 補足
75
+ `componentDidUpdate(prevProps, prevState, snapshot)`
76
+
77
+ [https://reactjs.org/docs/react-component.html#componentdidupdate](https://reactjs.org/docs/react-component.html#componentdidupdate)
78
+
79
+ 回答に載せたコードでは`componentWillUpdate`メソッドを使ってますが、React16.3以降では`componentWillUpdate`の利用を新規開発で利用するのは非推薦になっているので、代わりに`componentDidUpdate`で`state`の変更を確認するのが適切と言えます。(上の引用文の中でも`setState`メソッドのコールバックか、`componentDidMount`メソッド内で、、、と記載もありますね。)

8

a

2018/07/05 03:56

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -61,4 +61,12 @@
61
61
 
62
62
  単に`this.setState`によって`state`が変更されているかをログ出力して確認したいだけであれば、`this.setState`メソッドは第2引数に`state`の変更が完了した後に呼ばれるコールバック関数を渡すことが出来るので、そちらのほうでログ出力してあげると良いかと思います。
63
63
 
64
- もしくは、`state`が`update`された後に呼ばれるライフサイクルメソッド「`componentWillUpdate`」内で、変更後の`state`を確認しても良いかもしれません。
64
+ もしくは、`state`が`update`された後に呼ばれるライフサイクルメソッド「`componentWillUpdate`」内で、変更後の`state`を確認しても良いかもしれません。
65
+
66
+ # 参考
67
+
68
+ > Think of setState() as a request rather than an immediate command to update the component. For better perceived performance, React may delay it, and then update several components in a single pass. React does not guarantee that the state changes are applied immediately.
69
+
70
+ > setState() does not always immediately update the component. It may batch or defer the update until later. This makes reading this.state right after calling setState() a potential pitfall. Instead, use componentDidUpdate or a setState callback (setState(updater, callback)), either of which are guaranteed to fire after the update has been applied. If you need to set the state based on the previous state, read about the updater argument below.
71
+
72
+ [https://reactjs.org/docs/react-component.html#setstate](https://reactjs.org/docs/react-component.html#setstate)

7

componentWillUpdate

2018/07/05 03:50

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -59,4 +59,6 @@
59
59
 
60
60
  `this.setState`メソッドは非同期実行されるため、上記のように`this.setState`メソッドの実行直後に、`console.log`で変更後の`state`を確認しようとしても、その段階ではまだ、`state`の更新が実行すらされていません。そのため、結果は`null`となります。
61
61
 
62
- 単に`this.setState`によって`state`が変更されているかをログ出力して確認したいだけであれば、`this.setState`メソッドは第2引数に`state`の変更が完了した後に呼ばれるコールバック関数を渡すことが出来るので、そちらのほうでログ出力してあげると良いかと思います。
62
+ 単に`this.setState`によって`state`が変更されているかをログ出力して確認したいだけであれば、`this.setState`メソッドは第2引数に`state`の変更が完了した後に呼ばれるコールバック関数を渡すことが出来るので、そちらのほうでログ出力してあげると良いかと思います。
63
+
64
+ もしくは、`state`が`update`された後に呼ばれるライフサイクルメソッド「`componentWillUpdate`」内で、変更後の`state`を確認しても良いかもしれません。

6

["a", "b", "c"]

2018/07/05 03:46

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -19,10 +19,10 @@
19
19
  this.setState({
20
20
  items: newItems
21
21
  }, () => {
22
- console.log('callback', this.state.items); // ["a", "b", "c"]
22
+ console.log('callback', this.state.items); // ["a", "b", "c"]
23
-
24
23
  })
25
24
 
25
+ console.log('まだnullのまま', this.state.items) // null
26
26
  }
27
27
 
28
28
  componentWillUpdate(nextProps, nextState) {
@@ -40,7 +40,6 @@
40
40
  <App />,
41
41
  document.getElementById("root")
42
42
  );
43
-
44
43
  ```
45
44
 
46
45
  Demo => [https://codesandbox.io/s/qvzjm1q91q](https://codesandbox.io/s/qvzjm1q91q)

5

a

2018/07/05 03:43

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -19,13 +19,14 @@
19
19
  this.setState({
20
20
  items: newItems
21
21
  }, () => {
22
- console.log('callback', this.state.items);
22
+ console.log('callback', this.state.items); // ["a", "b", "c"]
23
+
23
24
  })
24
25
 
25
26
  }
26
27
 
27
28
  componentWillUpdate(nextProps, nextState) {
28
- console.log('componentWillUpdate', nextState.items);
29
+ console.log('componentWillUpdate', nextState.items); // ["a", "b", "c"]
29
30
  }
30
31
 
31
32
  render() {

4

a

2018/07/05 03:40

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -46,6 +46,10 @@
46
46
 
47
47
  # 解説
48
48
 
49
+ > 上記の感じで、配列をstate.countに入れたいのですが、nullのままになってしまいます。
50
+
51
+ 結論から言いますと、正しく`state`の更新自体は出来ています。単に、`console.log`を差し込むタイミングが後述の理由により、不適切であるため、`null`となってしまうだけの話です。
52
+
49
53
  ```
50
54
  this.setState({
51
55
  count: list

3

a

2018/07/05 03:38

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -46,6 +46,13 @@
46
46
 
47
47
  # 解説
48
48
 
49
+ ```
49
- `this.setState`メソッドは非同期実行されるため
50
+ this.setState({
51
+ count: list
52
+ });
53
+ console.log(this.state.count);//null
54
+ ```
50
55
 
56
+ `this.setState`メソッドは非同期実行されるため、上記のように`this.setState`メソッドの実行直後に、`console.log`で変更後の`state`を確認しようとしても、その段階ではまだ、`state`の更新が実行すらされていません。そのため、結果は`null`となります。
57
+
51
- なタイミンで保存投稿してしした。現在編集中)
58
+ 単に`this.setState`によって`state`が更されているかをロ出力して確認だけであれば、`this.setState`メソッドは第2引数に`state`の変更が完了した後に呼ばれるコールバック関数を渡すことが出来るのでそちらのほうでログ出力してあげると良いかと思います。

2

a

2018/07/05 03:37

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -42,4 +42,10 @@
42
42
 
43
43
  ```
44
44
 
45
- Demo => [https://codesandbox.io/s/qvzjm1q91q](https://codesandbox.io/s/qvzjm1q91q)
45
+ Demo => [https://codesandbox.io/s/qvzjm1q91q](https://codesandbox.io/s/qvzjm1q91q)
46
+
47
+ # 解説
48
+
49
+ `this.setState`メソッドは非同期実行されるため
50
+
51
+ (変なタイミングで保存投稿してしまいました。現在、編集中)

1

a

2018/07/05 03:33

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -1,3 +1,4 @@
1
+ # コード
1
2
  ```ここに言語を入力
2
3
  import React, { Component } from "react";
3
4
  import ReactDOM from "react-dom";
@@ -41,4 +42,4 @@
41
42
 
42
43
  ```
43
44
 
44
- https://codesandbox.io/s/qvzjm1q91q
45
+ Demo => [https://codesandbox.io/s/qvzjm1q91q](https://codesandbox.io/s/qvzjm1q91q)