从reactjs中的特定频道获取所有youtube视频
Get All The youtube video from specific channel in reactjs
我正在尝试使用 youtube 数据 api 从 youtube 中的特定频道获取视频 reactjs.But 我的 api 查询不 return anything.Here 是我为获得 video.What 所做的尝试是不是我做错了。
componentDidMount() {
console.log('componentDidMount colling ...');
fetch('https://www.googleapis.com/youtube/v3/channels?key="MYAPIKEY"&channelId="MYCHANNELID"&part=snippet,id&order=date&maxResults=2')
.then(results => {
this.setState({videos: results.json()});
this.setState({playingVideoId: this.state.videos[this.index]});
console.log("videos",this.state.videos)
console.log("videos",this.state.playingVideoId)
})
}
首先尝试使用邮递员查看URL是否返回正确的数据。
第二个:setState
是异步的这意味着,如果你console.log
在设置状态之后,你不会得到正确的结果。此外,您对 setState
的第二次调用使用的是第一次调用的数据,因此您不会得到正确的结果。只需调用 setState
一次并使用回调记录您得到的内容。
componentDidMount() {
console.log('componentDidMount colling ...');
fetch('https://www.googleapis.com/youtube/v3/channels?key="MYAPIKEY"&channelId="MYCHANNELID"&part=snippet,id&order=date&maxResults=2')
.then(results => {
const videosObj = results.json();
this.setState({
videos: videosObj,
playingVideoId: videosObj[this.index]
}, (updatedState) => {
console.log("videos", updatedState.videos);
console.log("videos", updatedState.playingVideoId);
});
})
}
我正在尝试使用 youtube 数据 api 从 youtube 中的特定频道获取视频 reactjs.But 我的 api 查询不 return anything.Here 是我为获得 video.What 所做的尝试是不是我做错了。
componentDidMount() {
console.log('componentDidMount colling ...');
fetch('https://www.googleapis.com/youtube/v3/channels?key="MYAPIKEY"&channelId="MYCHANNELID"&part=snippet,id&order=date&maxResults=2')
.then(results => {
this.setState({videos: results.json()});
this.setState({playingVideoId: this.state.videos[this.index]});
console.log("videos",this.state.videos)
console.log("videos",this.state.playingVideoId)
})
}
首先尝试使用邮递员查看URL是否返回正确的数据。
第二个:setState
是异步的这意味着,如果你console.log
在设置状态之后,你不会得到正确的结果。此外,您对 setState
的第二次调用使用的是第一次调用的数据,因此您不会得到正确的结果。只需调用 setState
一次并使用回调记录您得到的内容。
componentDidMount() {
console.log('componentDidMount colling ...');
fetch('https://www.googleapis.com/youtube/v3/channels?key="MYAPIKEY"&channelId="MYCHANNELID"&part=snippet,id&order=date&maxResults=2')
.then(results => {
const videosObj = results.json();
this.setState({
videos: videosObj,
playingVideoId: videosObj[this.index]
}, (updatedState) => {
console.log("videos", updatedState.videos);
console.log("videos", updatedState.playingVideoId);
});
})
}