I am calling api and getting response then setting response data to state. Problem is state is still undefined after setState and does not update Weather component with api data
class App extends React.Component {
state = {
temperature: undefined,
icon: undefined,
time: undefined,
timezone: undefined,
description: "123"
};
getWeather = async e => {
e.preventDefault();
const location = e.target.elements.city.value;
const api_call = await fetch(`${GEO_API_URL}${location}`);
const lngLat = await api_call.json();
console.log(lngLat);
const api_call2 = await fetch(
`${API_URL}${lngLat.latitude},${lngLat.longitude}/?lang=sl&units=si`
);
const forecast = await api_call2.json();
console.log(forecast);
this.setState = {
temperature: forecast.currently.temperature,
icon: forecast.currently.icon,
time: forecast.currently.time,
timezone: forecast.timezone,
description: forecast.currently.summary
};
console.log(this.state.temperature);
};
render() {
return (
<div>
<Form getWeather={this.getWeather} />
<Weather
temperature={this.state.temperature}
icon={this.state.icon}
time={this.state.time}
timezone={this.state.timezone}
description={this.state.description}
/>
</div>
);
}
}
export default App;