質問編集履歴

2

修正

2020/08/24 08:22

投稿

tomsuma
tomsuma

スコア38

test CHANGED
@@ -1 +1 @@
1
- チューブAPIを使って指定の動画を表示、再生した
1
+ 400 batRequest エラがなおらな
test CHANGED
File without changes

1

importの確認

2020/08/24 08:21

投稿

tomsuma
tomsuma

スコア38

test CHANGED
File without changes
test CHANGED
@@ -122,7 +122,7 @@
122
122
 
123
123
 
124
124
 
125
- export const fetchRelatedData = async (id) =>{
125
+ export const fetchRelatedData = async (id) =>{  データ輸送
126
126
 
127
127
  return await youtube.get('/search', {
128
128
 
@@ -138,7 +138,7 @@
138
138
 
139
139
  }
140
140
 
141
- export const fetchSearchData =async(query) =>{
141
+ export const fetchSearchData =async(query) =>{  データ輸送
142
142
 
143
143
  return await youtube.get('/search',{
144
144
 
@@ -155,3 +155,209 @@
155
155
  }
156
156
 
157
157
  ```
158
+
159
+
160
+
161
+ ```
162
+
163
+ Watch.js
164
+
165
+
166
+
167
+ import React,{useEffect,useContext} from 'react'
168
+
169
+ import Layout from '../components/Layout/Layout'
170
+
171
+ import VideoDetail from '../components/VideoDetail/VideoDetail'
172
+
173
+ import SideList from '../components/SideList/SideList'
174
+
175
+ import {Store} from '../store/index'
176
+
177
+ import {useLocation} from 'react-router-dom'
178
+
179
+ **import {fetchSelectedDate, fetchRelatedData} from '../apis/index'
180
+
181
+ **
182
+
183
+
184
+
185
+ const Watch = () => {
186
+
187
+ const {setGlobalState} = useContext(Store)
188
+
189
+ const location =useLocation()
190
+
191
+ const setVideos = async () =>{
192
+
193
+ const searchParams = new URLSearchParams(location.search)
194
+
195
+ const id = searchParams.get('v') データ取得?
196
+
197
+ if(id){
198
+
199
+ const [selected, related] = await Promise.all([fetchSelectedDate(id),fetchRelatedData(id)])
200
+
201
+ setGlobalState({type: 'SET_SELECTED', payload: {selected: selected.data.items.shift()}})
202
+
203
+ setGlobalState({type: 'SET_RELATED', payload: {related: related.data.items}})
204
+
205
+ }
206
+
207
+ }
208
+
209
+ useEffect(() => {
210
+
211
+ setVideos()
212
+
213
+ // eslint-disable-next-line react-hooks/exhaustive-deps
214
+
215
+ }, [location.search])
216
+
217
+ return (
218
+
219
+ <Layout>
220
+
221
+ <VideoDetail />
222
+
223
+ <SideList />
224
+
225
+ </Layout>
226
+
227
+ )
228
+
229
+ }
230
+
231
+
232
+
233
+ export default Watch
234
+
235
+ ```
236
+
237
+
238
+
239
+ ```
240
+
241
+ VideoDetail.js
242
+
243
+
244
+
245
+ import React,{useContext, useEffect} from 'react'
246
+
247
+ import {Store} from '../../store/index'
248
+
249
+ import VideoPlay from '../VideoPlay/VideoPlay'
250
+
251
+ import Styles from './VideoDetail.module.scss'
252
+
253
+ import Linkify from 'react-linkify';
254
+
255
+ import {useLocation} from 'react-router-dom'
256
+
257
+ import {**fetchSelectedDate}** from '../../apis/index'
258
+
259
+
260
+
261
+ const VideoDetail = () => {
262
+
263
+ const {globalState,setGlobalState} = useContext(Store)
264
+
265
+ const location = useLocation()
266
+
267
+ const setSelectedVideo = async () =>{
268
+
269
+ const searchParams = new URLSearchParams(location.search)
270
+
271
+ const id = searchParams.get('v')  データ取得?
272
+
273
+ await fetchSelectedDate(id).then((res) =>{
274
+
275
+ const item =res.data.items.shift()
276
+
277
+ setGlobalState({type: 'SET_SELECTED', payload: {selected: item}})
278
+
279
+ })
280
+
281
+ }
282
+
283
+ useEffect(() => {
284
+
285
+ setSelectedVideo()
286
+
287
+ // eslint-disable-next-line react-hooks/exhaustive-deps
288
+
289
+ },[])
290
+
291
+
292
+
293
+ return globalState.selected && globalState.selected.id?(
294
+
295
+ <div className={Styles.wrap}>   取得データの表示
296
+
297
+ <VideoPlay id ={globalState.selected.id}/>
298
+
299
+ <p>{globalState.selected.snippet.title}</p>
300
+
301
+ <hr />
302
+
303
+ <Linkify>
304
+
305
+ <pre>{globalState.selected.snippet.description}</pre>
306
+
307
+ </Linkify>
308
+
309
+ </div>
310
+
311
+ ) : (<span>no data</span>)
312
+
313
+ }
314
+
315
+
316
+
317
+ export default VideoDetail
318
+
319
+
320
+
321
+ ```
322
+
323
+
324
+
325
+ ```
326
+
327
+ VideoPlay.js   動画再生のためreact-youtube'インストール
328
+
329
+
330
+
331
+
332
+
333
+ import React from 'react'
334
+
335
+ import Youtube from 'react-youtube'
336
+
337
+ import Style from './VideoPlay.module.scss'
338
+
339
+
340
+
341
+ const VideoPlay = ({id}) =>{
342
+
343
+
344
+
345
+ return (
346
+
347
+ <div className={Style.wrap}>
348
+
349
+ <Youtube className={Style.video} videoId ={id}/>
350
+
351
+ </div>
352
+
353
+ )
354
+
355
+ }
356
+
357
+
358
+
359
+ export default VideoPlay
360
+
361
+
362
+
363
+ ```