回答編集履歴
3
a
test
CHANGED
@@ -46,7 +46,7 @@
|
|
46
46
|
|
47
47
|
|
48
48
|
|
49
|
-
実際の業務では、keroxpさんの回答にあるように`axios`を使うか、もしくは`RxJS`を使うのが一般的かと思います。
|
49
|
+
また、今回は質問の内容に寄せて、そのまま`XMLHttpRequest`を使用しての回答をしましたが、実際の業務では、keroxpさんの回答にあるように`axios`を使うか、もしくは(特に国外では)`RxJS`を使うのが一般的かと思います。
|
50
50
|
|
51
51
|
|
52
52
|
|
2
a
test
CHANGED
@@ -22,6 +22,8 @@
|
|
22
22
|
|
23
23
|
リクエストが完了して結果が帰ってきた後に`JSON.parse()`を実行してあげれば大丈夫かと思います。
|
24
24
|
|
25
|
+
[https://codesandbox.io/s/yv3z86wl11](https://codesandbox.io/s/yv3z86wl11)
|
26
|
+
|
25
27
|
|
26
28
|
|
27
29
|
# 参考
|
@@ -31,3 +33,149 @@
|
|
31
33
|
- [https://developer.mozilla.org/ja/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest](https://developer.mozilla.org/ja/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest)
|
32
34
|
|
33
35
|
- [https://uhyohyo.net/javascript/13_1.html](https://uhyohyo.net/javascript/13_1.html)
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
# 追記
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
keroxpさんの回答にあるように`render`メソッド内でAPIリクエストを行うのはよろしくありません。
|
44
|
+
|
45
|
+
パッとコードを見て回答をしてしまったので見落としていましたが、後から見る人用に回答に反映させておくべき点だと思いましたので、その部分を反映させたコードを追記させていただきます。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
実際の業務では、keroxpさんの回答にあるように`axios`を使うか、もしくは`RxJS`を使うのが一般的かと思います。
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
import React from 'react';
|
56
|
+
|
57
|
+
import ReactDOM from 'react-dom';
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
class NewsList extends React.Component {
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
constructor(props) {
|
66
|
+
|
67
|
+
super(props);
|
68
|
+
|
69
|
+
this.state = {
|
70
|
+
|
71
|
+
topStories: []
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
this.xhr = new XMLHttpRequest();
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
componentDidMount() {
|
82
|
+
|
83
|
+
this.xhr.open("GET", "https://hacker-news.firebaseio.com/v0/topstories.json", true);
|
84
|
+
|
85
|
+
this.xhr.setRequestHeader("Content-type", "application/json");
|
86
|
+
|
87
|
+
this.xhr.send();
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
this.xhr.addEventListener('load', this.handleOnLoad);
|
92
|
+
|
93
|
+
this.xhr.addEventListener('error', this.handleOnError);
|
94
|
+
|
95
|
+
this.xhr.addEventListener('abort', this.handleOnAbort);
|
96
|
+
|
97
|
+
this.xhr.addEventListener('timeout', this.handleOnTimeout);
|
98
|
+
|
99
|
+
this.xhr.addEventListener('progress', this.handleOnProgress);
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
handleOnLoad = (event) => {
|
106
|
+
|
107
|
+
try {
|
108
|
+
|
109
|
+
const responseData = JSON.parse(this.xhr.responseText);
|
110
|
+
|
111
|
+
this.setState({ topStories: responseData })
|
112
|
+
|
113
|
+
} catch (error) {
|
114
|
+
|
115
|
+
console.error(error);
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
handleOnError = (event) => {}
|
124
|
+
|
125
|
+
handleOnAbort = (event) => {}
|
126
|
+
|
127
|
+
handleOnTimeout = (event) => {}
|
128
|
+
|
129
|
+
handleOnProgress = (event) => {}
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
componentWillUnmount() {
|
134
|
+
|
135
|
+
this.xhr.abort();
|
136
|
+
|
137
|
+
this.xhr.removeEventListener('load', this.handleOnLoad);
|
138
|
+
|
139
|
+
this.xhr.removeEventListener('error', this.handleOnError);
|
140
|
+
|
141
|
+
this.xhr.removeEventListener('abort', this.handleOnAbort);
|
142
|
+
|
143
|
+
this.xhr.removeEventListener('timeout', this.handleOnTimeout);
|
144
|
+
|
145
|
+
this.xhr.removeEventListener('progress', this.handleOnProgress);
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
render() {
|
152
|
+
|
153
|
+
return (
|
154
|
+
|
155
|
+
<ol>
|
156
|
+
|
157
|
+
{
|
158
|
+
|
159
|
+
this.state.topStories.map((storyId) => {
|
160
|
+
|
161
|
+
return <li key={storyId}>{storyId}</li>
|
162
|
+
|
163
|
+
})
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
</ol>
|
168
|
+
|
169
|
+
)
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
ReactDOM.render(<NewsList />, document.getElementById('root'));
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
```
|
1
a
test
CHANGED
@@ -29,3 +29,5 @@
|
|
29
29
|
- [https://developer.mozilla.org/ja/docs/Web/API/XMLHttpRequest](https://developer.mozilla.org/ja/docs/Web/API/XMLHttpRequest)
|
30
30
|
|
31
31
|
- [https://developer.mozilla.org/ja/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest](https://developer.mozilla.org/ja/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest)
|
32
|
+
|
33
|
+
- [https://uhyohyo.net/javascript/13_1.html](https://uhyohyo.net/javascript/13_1.html)
|