我具有提取数据的功能,并且执行setState
将数据放入自己的状态。
但是我不知道如何在render()
getFixture = async () => {
const url = 'http://127.0.0.1:80/testmatch/get_fixture.php';
fetch(url).then((reponse) => reponse.json())
.then((reponseJson) => {
this.setState({
data:reponseJson,isLoading:false
})
})
}
componentDidmount(){
this.getFixture()
}
如果我在渲染中执行了console.log(this.state.data);
,则在控制台中(在图像连接中)向我展示了这一点
如果我做console.log(this.state.data.elapsed);
可以正常工作并且可以获取值,但是如果我做console.log(this.state.data.awayTeam.logo);
可以得到 无法读取属性“徽标”
我不明白为什么。
这是所有代码,我想要的是使用从数据状态到渲染的所有数据:
getFixture = async () => {
const url = 'http://127.0.0.1:80/testmatch/get_fixture.php';
fetch(url).then((reponse) => reponse.json())
.then((reponseJson) => {
this.setState({
data:reponseJson,isLoading:false
})
console.log(this.state.data.awayTeam.logo); // WORKING (SHOW ME THE LINK)
})
}
render() {
console.log(this.state.data.awayTeam.logo); // NOT WORKING (CANNOT READ PROPERTY OF UNDEFINED)
return (
<View style={styles.container}>
<View style={styles.containerScore}>
<View style={{flex: 1,flexDirection: 'row',borderColor:'#171F33',borderBottomWidth: 1 }}>
<View style={{flex: 1,paddingTop:7,paddingBottom:7}}>
<View style={{flex: 1}}>
<Image style={{width: 50,height: 50}} source={{uri: "toto"}} />
</View>
</View>
<View style={{flex: 1,paddingBottom:7,backgroundColor:'#fff'}}>
<Text style={{fontSize:13}}> {this.state.data.elapsed}</Text>
</View>
<View style={{flex: 1,marginLeft:2,backgroundColor:'#000'}}>
<Text style={{fontSize:13}}></Text>
</View>
</View>
<Text style={{color:"#FFF"}}>{} </Text>
</View>
<View style={styles.containerInfo}>
<ScrollabletabView style={{marginTop: 1}} initialPage={0} renderTabBar={() => <CustomTabBar/>} >
<View tabLabel='Tab #1'>
<Text>My</Text>
</View>
<View tabLabel='Tab #2'>
<Text>favorite</Text>
</View>
<View tabLabel='Tab #3'>
<Text>favorite</Text>
</View>
<View tabLabel='Tab #4'>
<Text>favorite</Text>
</View>
</ScrollabletabView>
</View>
</View>
);
}
}