【发布时间】:2021-01-27 20:40:39
【问题描述】:
我是 React 和开发 covid-19 跟踪器应用程序的新手。我有一个国家/地区下拉列表,当我们从下拉列表中选择任何国家/地区时,将进行 api 调用,并使用反应挂钩(useState)显示电晕病例、死亡病例和康复病例。使用 onCountryChange 函数会发生这种情况;
const onCountryChange = async (event) => {
const countryCode = event.target.value;
setCountry(countryCode);
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);
setCountryInfo(data);
setMapCenter([data.countryInfo.lat, data.countryInfo.long]);
setMapZoom(4);
});
};
我已经引入了 react 传单地图并有一个状态
[ mapCenter, setMapCenter ] = useState({lat: 34.80746, lng: -40.4796})
现在在 onCountryChange 我正在设置 setMapCenter([data.countryInfo.lat, data.countryInfo.long]);但地图未以所选国家/地区为中心。任何人都可以帮助我这里有什么问题。 这是 github 仓库。
https://github.com/sohailshams/covid-19-tracker
【问题讨论】:
标签: reactjs leaflet react-leaflet