【发布时间】:2020-05-06 11:39:53
【问题描述】:
我仍然是使用 react native 的新手,我遇到了这样的问题: 我试图从这个 api 获取数据,它是 https://covid19.mathdro.id/api 只是为了获得死亡患者的价值,但我在应用程序中什么都没有,它没有显示死亡的价值,但在控制台中它显示了价值和细节 这是我使用的代码:
class Deaths extends React.Component{
constructor(){
super();
this.state = {
Dead: [
],
refreshing: false
}
}
}
renderItem = ({item}) => (
<View>
<Text>{item.value}</Text>
<Text>{item.detail}</Text></View>
)
onRefresh = () => {
this.getDataApi();
}
componentDidMount = () => {
this.getDataApi();
}
getDataApi = async () =>{
this.setState({ refreshing: true})
const response = await fetch('https://covid19.mathdro.id/api/')
const json = await response.json();
this.setState({Dead: json.deaths, refreshing: false})
}
render(){
console.log(this.state.Dead)
return(
<View>
<FlatList
data={this.state.Dead}
keyExtractor={item => item.deaths.toString()}
renderItem={this.renderItem}
refreshing = {this.state.refreshing}
onRefresh={this.onRefresh}
/>
</View>
)
}
【问题讨论】:
标签: react-native