回答編集履歴
5
テキスト修正
test
CHANGED
@@ -64,7 +64,7 @@
|
|
64
64
|
|
65
65
|
|
66
66
|
|
67
|
-
[This is why we need to bind event handlers in Class Components in React](https://medium.freecodecamp.org/this-is-why-we-need-to-bind-event-handlers-in-class-components-in-react-f7ea1a6f93eb)
|
67
|
+
- [This is why we need to bind event handlers in Class Components in React](https://medium.freecodecamp.org/this-is-why-we-need-to-bind-event-handlers-in-class-components-in-react-f7ea1a6f93eb) (Saurabh Misraさん)
|
68
68
|
|
69
69
|
|
70
70
|
|
@@ -85,3 +85,7 @@
|
|
85
85
|
|
86
86
|
|
87
87
|
- [ES6 で this が変わらないメソッド](https://qiita.com/hakomo/items/df7f3ba67e4b147b410e) (hakomoさん@Qiita)
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
- [Binding Methods to Class Instance Objects](https://ponyfoo.com/articles/binding-methods-to-class-instance-objects) (Nicolásさん)
|
4
テキスト修正
test
CHANGED
@@ -71,3 +71,17 @@
|
|
71
71
|
|
72
72
|
|
73
73
|
以上、参考になれば幸いです。
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
---
|
78
|
+
|
79
|
+
**追記**
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
以下も参考になるかもしれません。
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
- [ES6 で this が変わらないメソッド](https://qiita.com/hakomo/items/df7f3ba67e4b147b410e) (hakomoさん@Qiita)
|
3
テキスト修正
test
CHANGED
@@ -28,7 +28,7 @@
|
|
28
28
|
|
29
29
|
|
30
30
|
|
31
|
-
以下、`this.tick = this.tick.bind(this);`が必要な理由です。こ
|
31
|
+
以下、`this.tick = this.tick.bind(this);`が必要な理由です。これを追加しないと、
|
32
32
|
|
33
33
|
|
34
34
|
|
2
テキスト修正
test
CHANGED
@@ -20,22 +20,54 @@
|
|
20
20
|
|
21
21
|
```javascript
|
22
22
|
|
23
|
-
this.timerID = setInterval(
|
24
|
-
|
25
|
-
() => this.tick(),
|
26
|
-
|
27
|
-
1000
|
28
|
-
|
29
|
-
);
|
30
|
-
|
31
|
-
```
|
32
|
-
|
33
|
-
の部分を
|
34
|
-
|
35
|
-
```javascript
|
36
|
-
|
37
23
|
this.timerID = setInterval(this.tick, 1000);
|
38
24
|
|
39
25
|
```
|
40
26
|
|
41
|
-
|
27
|
+
が意図通り動作します。
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
以下、`this.tick = this.tick.bind(this);`が必要な理由です。この行が無いと、
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
```javascript
|
36
|
+
|
37
|
+
tick() {
|
38
|
+
|
39
|
+
this.setState({
|
40
|
+
|
41
|
+
date: new Date()
|
42
|
+
|
43
|
+
});
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
の `this.setState()` の `this` はグローバルオブジェクトを参照し、ブラウザで実行されるならば `window` になります。`window`には `setState`というメソッドはないので、
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
Uncaught TypeError: this.setState is not a function at tick
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
というエラーになってしまいます。
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
上記について、(英語ですが)以下の記事が分かりやすいです。
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
[This is why we need to bind event handlers in Class Components in React](https://medium.freecodecamp.org/this-is-why-we-need-to-bind-event-handlers-in-class-components-in-react-f7ea1a6f93eb)
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
以上、参考になれば幸いです。
|
1
テキスト修正
test
CHANGED
@@ -1,10 +1,4 @@
|
|
1
1
|
こんにちは。
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
質問への追記・修正に書いたとおり、ご質問のコードそのままでも意図通り動きますが、以下を補足します。
|
6
|
-
|
7
|
-
|
8
2
|
|
9
3
|
コンストラクターの処理に `this.tick = this.tick.bind(this);` を追加して、以下
|
10
4
|
|
@@ -22,11 +16,7 @@
|
|
22
16
|
|
23
17
|
```
|
24
18
|
|
25
|
-
|
26
|
-
|
27
19
|
のようにすることによって、
|
28
|
-
|
29
|
-
|
30
20
|
|
31
21
|
```javascript
|
32
22
|
|
@@ -40,7 +30,7 @@
|
|
40
30
|
|
41
31
|
```
|
42
32
|
|
43
|
-
の部分
|
33
|
+
の部分を
|
44
34
|
|
45
35
|
```javascript
|
46
36
|
|