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

回答編集履歴

5

テキスト修正

2018/08/12 00:36

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -31,7 +31,7 @@
31
31
 
32
32
  上記について、(英語ですが)以下の記事が分かりやすいです。
33
33
 
34
- [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)
34
+ - [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さん)
35
35
 
36
36
 
37
37
  以上、参考になれば幸いです。
@@ -41,4 +41,6 @@
41
41
 
42
42
  以下も参考になるかもしれません。
43
43
 
44
- - [ES6 で this が変わらないメソッド](https://qiita.com/hakomo/items/df7f3ba67e4b147b410e) (hakomoさん@Qiita)
44
+ - [ES6 で this が変わらないメソッド](https://qiita.com/hakomo/items/df7f3ba67e4b147b410e) (hakomoさん@Qiita)
45
+
46
+ - [Binding Methods to Class Instance Objects](https://ponyfoo.com/articles/binding-methods-to-class-instance-objects) (Nicolásさん)

4

テキスト修正

2018/08/12 00:36

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -34,4 +34,11 @@
34
34
  [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)
35
35
 
36
36
 
37
- 以上、参考になれば幸いです。
37
+ 以上、参考になれば幸いです。
38
+
39
+ ---
40
+ **追記**
41
+
42
+ 以下も参考になるかもしれません。
43
+
44
+ - [ES6 で this が変わらないメソッド](https://qiita.com/hakomo/items/df7f3ba67e4b147b410e) (hakomoさん@Qiita)

3

テキスト修正

2018/08/12 00:20

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -13,7 +13,7 @@
13
13
  ```
14
14
  が意図通り動作します。
15
15
 
16
- 以下、`this.tick = this.tick.bind(this);`が必要な理由です。この行が無いと、
16
+ 以下、`this.tick = this.tick.bind(this);`が必要な理由です。これを追加しないと、
17
17
 
18
18
  ```javascript
19
19
  tick() {

2

テキスト修正

2018/08/11 23:44

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -9,13 +9,29 @@
9
9
  ```
10
10
  のようにすることによって、
11
11
  ```javascript
12
- this.timerID = setInterval(
12
+ this.timerID = setInterval(this.tick, 1000);
13
- () => this.tick(),
14
- 1000
15
- );
16
13
  ```
17
- の部分を
14
+ が意図通り動作します。
15
+
16
+ 以下、`this.tick = this.tick.bind(this);`が必要な理由です。この行が無いと、
17
+
18
18
  ```javascript
19
+ tick() {
19
- this.timerID = setInterval(this.tick, 1000);
20
+ this.setState({
21
+ date: new Date()
22
+ });
23
+ }
20
24
  ```
25
+
26
+ の `this.setState()` の `this` はグローバルオブジェクトを参照し、ブラウザで実行されるならば `window` になります。`window`には `setState`というメソッドはないので、
27
+
28
+ Uncaught TypeError: this.setState is not a function at tick
29
+
21
- 書けます。
30
+ いうエラーになってしいます。
31
+
32
+ 上記について、(英語ですが)以下の記事が分かりやすいです。
33
+
34
+ [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)
35
+
36
+
37
+ 以上、参考になれば幸いです。

1

テキスト修正

2018/08/11 23:35

投稿

jun68ykt
jun68ykt

スコア9058

answer CHANGED
@@ -1,7 +1,4 @@
1
1
  こんにちは。
2
-
3
- 質問への追記・修正に書いたとおり、ご質問のコードそのままでも意図通り動きますが、以下を補足します。
4
-
5
2
  コンストラクターの処理に `this.tick = this.tick.bind(this);` を追加して、以下
6
3
  ```javascript
7
4
  constructor(props) {
@@ -10,16 +7,14 @@
10
7
  this.tick = this.tick.bind(this);
11
8
  }
12
9
  ```
13
-
14
10
  のようにすることによって、
15
-
16
11
  ```javascript
17
12
  this.timerID = setInterval(
18
13
  () => this.tick(),
19
14
  1000
20
15
  );
21
16
  ```
22
- の部分は、より短いコードで
17
+ の部分
23
18
  ```javascript
24
19
  this.timerID = setInterval(this.tick, 1000);
25
20
  ```