我不断得到
TypeError:country.map不是函数
,我不知道为什么。当我在<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
答案 0 :(得分:1)
就像const [tableData, setTableData] = useState({});
一样,您正在将tableData
设置为对象。
另外,API的结果是作为对象的tableData
数据,再次设置为对象
setTableData(data);
使用<Table countries={tableData} />
组件时,tableData
是对象而不是数组。
答案 1 :(得分:1)
LoginResponse(user, accessToken)
答案 2 :(得分:0)
您还可以添加检查国家/地区是否存在。
countries && countries.map(({ country, cases }) => {}