I am learning react and making a weather app. It is fetching API from open weather and assigns response to data and then setting value to city using setCity. But on first load the city varaible is undefined and when I console log out data it has all the JSON object.
const [city, setCity] = useState({})
useEffect(()=>{
getWeather()
},[setCity])
const getWeather = async ()=> {
const reqAPI = `http://api.openweathermap.org/data/2.5/weather?q=toronto&units=metric&appid=${API_KEY}`
const response = (await fetch(reqAPI)).json()
const data = await response
console.log(data)
setCity(data)
console.log(city)
}
console log data is ok giving all value but city is undefined on first load and crashes the app. Please help as I am not able to find a solution
Here is the console output both should be same but are not!
You can see after editing out the code I am still getting this error