javascript - 使用 react js 渲染 youtube 视频
问题描述
我的目标是将查询提供给 youtube 搜索并在我的简单反应 js 应用程序中呈现检索视频。我在npm start
这个反应组件之后,本地主机中什么都没有出现。在控制台中我不断地遇到这个错误,我使用 axios 从 API 获取数据:
Yutube.js:23 Error: Request failed with status code 400
at createError (createError.js:16)
at settle (settle.js:18)
at XMLHttpRequest.handleLoad (xhr.js:77)
这是我的 YoutubeRitriver react JS 组件:
import React, { Component } from 'react';
import axios from 'axios';
export default class YouTube extends Component {
constructor(props) {
super(props);
this.state = {video: []};
}
componentWillMount() {
setInterval(() => {
this.VideoList();
}, 2000)
}
VideoList() {
axios.get('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=UUbW18JZRgko_mOGm5er8Yzg&key={MY DATA GOOGLE KEY')
.then((response) => {
this.setState({video: response.headers});
})
.catch(function (error) {
console.log(error);
});
}
render() {
return (
<div id="layout-content" className="layout-content-wrapper">
<div className="panel-list">
{this.state.video.map((item, i) =>{
return(
<h1>{item.items}</h1>
)
})}
</div>
</div>
)
}
}
解决方案
我认为你必须将你的 VideoList 方法作为异步操作的中间件来处理,因为你在 ComponentWillMount 中执行它,如果你的间隔计时器永远不会达到,因为反应建议停止使用 ComponentWillMount,你可以在 ComponentDidMount 中尝试你的异步操作,但我们会看到为了更好的答案:-) ..
推荐阅读
- java - 对话出现在默认消息应用程序中
- vim - 如何确定 Vim 中的当前窗口是否在新选项卡中打开?
- python - 未找到请求的 URL。pythonanywhere中的404错误
- java - 调用 REST API 的次数
- python - python3 itertools.filterfalse 非常慢。有哪些替代方案?
- python - 将列表拆分为 N 部分以获得元组列表
- c++ - 如何在这里运行while循环?
- python - 在多次碰撞后附加项目
- javascript - 如何在Angular 6中单击按钮时获取ngPrime自动完成文本
- state - 如何从其主体内更新视图的复杂模型