javascript - React API Fetch 不返回所有数据,但在索引时返回
问题描述
我的问题是我在 FetchData 类中获得了一个数据列表,购买它的列表。我尝试为国家/地区编制索引: data.Countries[0]例如 0 它给了我值但我想要所有数据有没有办法对此进行foreach?
fetchData.js
import React, { Component } from 'react';
import Countries from './countries';
class FetchData extends Component {
state = {
loading: true,
countries: null
};
async componentDidMount() {
const url = 'https://api.covid19api.com/summary';
const response = await fetch(url);
const data = await response.json();
this.setState({
countries: data.Countries,
loading: false
});
}
render() {
return (
<div>
{this.state.loading || !this.state.countries ? (
<div>loading</div>
) : (
<div>
<Countries WorldCountries={this.state.countries.Country} />
</div>
)}
</div>
);
}
}
export default FetchData;
国家.js
import React, { Component } from 'react';
const Countries = (prop) => {
return(
<h1>{prop.Countries}</h1>
);
}
export default Countries;
解决方案
您的图片让我感到困惑,但如果我理解正确,您需要执行以下操作:
// FetchData component
...
render() {
return (
<div>
{this.state.loading || !this.state.countries ? (
<div>loading</div>
) : (
<div>
{
this.state.countries.map(country =>
<Country country={country.Country} />)
}
</div>
)}
</div>
);
}
...
// FetchData component
// Country component
const Countries = ({ country }) => (
<h1>{country}</h1>
);
// Country component
推荐阅读
- flutter - 如何在某个区域画一条线,并从它停止的地方继续
- git - 如何让 Redmine 更新其数据库以反映配置的 git 存储库的内容
- reactjs - 如何使用 promise 递归地获取分页数据?
- python - 如何在箱线图中显示所有年龄细分
- laravel - 如何摆脱我的 Laravel 网站上的 aeR4Choc(恶意代码)?
- c++ - 从 Arduino 中的函数返回初始未知长度的 char 数组
- php - 如何在 Laravel 中拥有不同的 robots.txt 文件子域和主域?
- javascript - Docusign:如何让信封收件人在不同时间签署两个文件?
- php - 如何使用 PHP 在 netsuite api 中通过销售代表获取销售订单项目?
- python - Python - 从字典类型列熊猫数据框中获取前 5 项