guys I'm working on weather app and I struggle with useState asynchronous. I've read on stackoverflow that it can be solved with callback in useState function. So I've tried it but nothing changed. The issue is that when I click on submit button, state must receive data from API but it doesnt make it and I'm getting null in my state. What is wrong and if u can pls help me according to my code. P.S missed some funcrtions that are not important
const useForecast = () => {
const [forecast, setForecast] = useState(null);
const gatherForecastData = async data => {
const currentDay = await getCurrentDayForecast(data.consolidated_weather[0], data.title);
const currentDayDetails = await getCurrentDayDetailedForecast(data.consolidated_weather[0]);
await setForecast(() => {
return { currentDay, currentDayDetails};
});
setLoading(false);
};
// MAIN FUNC AND TROUBLE IS HERE
const submitRequest = async location => {
setLoading(true);
setError(false);
const response = await getWoeid(location);
if (!response?.woeid) return;
const data = await getForecastData(response.woeid);
if (!data) return;
await gatherForecastData(data);
console.log(data);
// DATA SHOWS OK
console.log(forecast);
// SHOWS INITIAL STATE FOR THE FIRST SUBMIT
};
}