嗨,我想在react-native中访问我的json数组的属性videoId'
,以便显示视频,但出现错误无法读取未定义的属性'videoId'。这是我对dataVideos[0]
的回复:
我正在尝试获取:
youtubeDisplay(){
<View style = {{height:200}}>
<YouTube
ref={this._youTubeRef}
apiKey= {this.apiKey}
videoId= {this.state.dataVideos[0].contentDetails.videoId}
play={this.state.isPlaying}
loop={this.state.isLooping}
fullscreen={this.state.fullscreen}
controls={1}
style={[
{ height: PixelRatio.roundToNearestPixel(this.state.playerWidth / (16 / 9)) },
styles.player,
]}
onError={e => {
this.setState({ error: e.error });
}}
onReady={e => {
this.setState({ isReady: true });
}}
onChangeState={e => {
this.setState({ status: e.state });
}}
onChangeQuality={e => {
this.setState({ quality: e.quality });
}}
onChangeFullscreen={e => {
this.setState({ fullscreen: e.isFullscreen });
}}
onProgress={e => {
this.setState({ currentTime: e.currentTime });
}}
/>
</View>
}
如何访问videoId
?谢谢
EDIT:这是我从API获得响应的代码。
_fetchPlaylist(){
if(this.state.selectedIndex === 0) {
return(
fetch(`https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&maxResults=${results}&playlistId=PLSlVQ0kIy6qx-f5O3J3GwIEOO5Y52z43-&key=${apiKey}`)
.then(res => res.json())
.then(result => {
const videoId = []
result.items.forEach(item => {
videoId.push(item)
})
this.setState({
dataVideos:videoId
});
})
.catch(error => {
console.error(error)
})
)
}
}
答案 0 :(得分:0)
此日志是this.state.dataVideos [0]吗?尝试使用提供lYigBSIZ6B0
之类的手动ID(如果可行),然后检查您的状态,也许您在运行该方法之前错过了setstate。
答案 1 :(得分:0)
尝试访问componentWillMount()中的videoId