【发布时间】:2021-10-28 23:08:20
【问题描述】:
我想迭代将重复卡片的数组。我收到了回复
{
"get":"statistics"
"parameters":{...}
"errors":[]
"results":1
"response":[...]
}
我想迭代响应并在response 内部尝试显示population
响应数组(内部)
"response":[
{
"continent":"Oceania"
"country":"Australia"
"population":25842408
"cases":{...}
"deaths":{...}
"tests":{...}
"day":"2021-08-30"
"time":"2021-08-30T06:45:03+00:00"
}
]
当我尝试在控制台中显示它时它可以工作并显示人口但是当我使用map() 函数重复卡片时它会给出未定义不是对象的错误
我在哪里做错了
我的班级
export default class Covidscreen extends React.Component{
constructor(props){
super(props)
this.state = {
results :{},
namee:'fahad',
};
}
static navigationOptions = {
title: 'Loginscreen',
//Sets Header text of Status Bar
headerStyle: {
backgroundColor: '#f4511e',
//Sets Header color
},
headerTintColor: '#fff',
//Sets Header text color
headerTitleStyle: {
fontWeight: 'bold',
//Sets Header text style
},
};
getCovidData = () => {
const options = {
method: 'GET',
params: {country: 'australia'},
url: 'https://covid-193.p.rapidapi.com/statistics',
headers: {
'x-rapidapi-host': 'covid-193.p.rapidapi.com',
'x-rapidapi-key': 'e4b2e7d44bmsh5427dd9c7fb16dbp15d226jsn1fdf62e5a45e'
}
};
axios.request(options)
.then((response) => {
this.setState({results:response.data});
})
.catch(function (error) {
console.error(error);
});
}
componentDidMount() {
this.getCovidData()
}
render() {
const { navigate } = this.props.navigation;
return (
<View >
{ this.state.results.response.map(row => (
<Card >
<Card.Title>{row.population}</Card.Title>
<Card.Divider/>
</Card>
))}
</View>
)
}
}
我正在使用基于类的组件,并且我已经记录了响应,并且它在我尝试循环和访问人口时在那里工作
【问题讨论】:
标签: javascript reactjs react-native axios