我不断收到错误:“TypeError:countries.map 不是一个函数”
2020-07-24
1548
我不断收到
TypeError: countries.map is not a function
,但我不知道为什么。当我注释掉
<Table />
并使用
console.log(tableData)
时,它会显示正确的国家/地区数组,但是当我包含
<Table />
时,我没有得到数组。
任何帮助都将不胜感激!
import React, {useState, useEffect} from 'react';
import {
MenuItem,
FormControl,
Select,
Card,
CardContent
} from "@material-ui/core";
import InfoBox from "./InfoBox";
import Map from "./Map";
import './App.css';
import Table from "./Table";
function App() {
const [countries, setCountries] = useState([]);
const [country, setCountry] = useState(["worldwide"]);
const [countryInfo, setCountryInfo] = useState({});
const [tableData, setTableData] = useState({});
useEffect(() => {
fetch('https://disease.sh/v3/covid-19/all')
.then(response => response.json())
.then((data) => {
setCountryInfo(data);
})
}, []);
useEffect(() => {
const getCountriesData = async () => {
await fetch("https://disease.sh/v3/covid-19/countries")
.then((response) => response.json())
.then((data) => {
const countries = data.map((country) => (
{
name: country.country,
value: country.countryInfo.iso2
}
));
setTableData(data);
setCountries(countries);
});
};
getCountriesData();
}, []);
const onCountryChange = async (event) => {
const countryCode = event.target.value;
const url = countryCode === 'worldwide' ? 'https://disease.sh/v3/covid-19/all' : `https://disease.sh/v3/covid-19/countries/${countryCode}`;
await fetch(url)
.then(response => response.json())
.then(data => {
setCountry(countryCode);
//All data from country
setCountryInfo(data);
})
};
console.log('COUNTRY INFO >>>', countryInfo);
return (
<div className="app">
<div className="app__left">
<div className="app__header">
<h1>COVID-19 STATS</h1>
<FormControl className="app__dropdown">
<Select variant="outlined" onChange={onCountryChange} value={country}>
<MenuItem value="worldwide">Worldwide</MenuItem>
{
countries.map((country) => (
<MenuItem value={country.value}>{country.name}</MenuItem>
))
}
</Select>
</FormControl>
</div>
<div className="app__stats">
<InfoBox title="COVID-19 Cases" cases={countryInfo.todayCases} total={countryInfo.cases} />
<InfoBox title="Recovered" cases={countryInfo.todayRecovered} total={countryInfo.recovered}/>
<InfoBox title="Deaths" cases={countryInfo.todayDeaths} total={countryInfo.deaths} />
</div>
<Map />
</div>
<Card className="app__right">
<CardContent>
<h3>Live cases per country</h3>
{console.log(tableData)}
<Table countries={tableData} />
<h3>Worldwide new cases</h3>
</CardContent>
</Card>
</div>
);
}
export default App;
这是 Table.js 文件:
import React from 'react'
function Table({ countries }) {
return <div className="table">
{countries.map(({ country, cases }) => (
<tr>
<td>{country}</td>
<td>{cases}</td>
</tr>
))}
</div>
}
export default Table
3个回答
看起来像
const [tableData, setTableData] = useState({});
,您正在将
tableData
设置为对象。
此外,API 结果数据是一个对象,而
tableData
再次设置为对象
setTableData(data);
当使用
<Table countries={tableData} />
组件时,
tableData
是一个对象,而不是数组。
brijesh-pant
2020-07-24
Check the sort function used you must have returned something wrong
export const sortData = (data) => {
const sortedData = [...data]; //copy the data to sortedData
sortedData.sort((a, b) => {
if (a.cases > b.cases) {
return -1;
} else {
return 1;
}
});
return sortedData;
};
Smit Desai
2021-03-22
您还可以添加检查国家是否存在。
countries && countries.map(({ country, cases }) => {}
Shivang Gupta
2020-07-24