I am trying to change the state of my map. My code is quite simple. But i can't figure out why it's not working.
const [mapCenter, setMapCenter] = useState({ lat: 34.80746, lng: -40.4796 });
const onCountryChange = async (e) => {
const countryCode = e.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);
setCountryInfo(data);
console.log(data.countryInfo.lat, data.countryInfo.lng)
setMapCenter([data.countryInfo.lat, data.countryInfo.lng]);
setMapZoom(6)
console.log(mapCenter, mapZoom)
})
}
In my onCountryChange function, I am calling data from an API and setting the data in the UI, which is working fine, but the setMapCenter data fails to update. When I log the response I get from the API, I get the correct response in the console, but when I log the state of mapCenter, it still returns the initial state... what am I doing wrong please?