How do I wait and/or build a return with values that come from an API-Call via axios in react?
The problem im Facing is that axios requests are asynchronous. Thats why the return from my code is hit earlier then the response from my request arrives. I'm trying to build Cards from a Weather API (openweathermap) that are returned when you enter certain parameters.
Here is how far I got. Hope you can help me:
function WeatherTile() {
let data;
const options = {
method: 'GET',
url: 'https://api.openweathermap.org/data/2.5/weather',
params: {
appid: WEATHER_API_KEY,
id: '2924585',
lang: 'null',
units: '"metric" or "imperial"',
mode: 'xml, html'
}
};
axios.request(options).then(function(response) {
if(response.status == 200){
mobxInteractionStore.setWeatherdata(response.data); // mobx doesnt work either. Its empty when the html is returned.
console.log("SUCCESS");
console.log(response.data);
data = response.data; // This does not work
}else {
console.log(response.status);
}
}).catch(function(error) {
console.error(error);
});
return (
<Card style={{ width: '18rem' }}>
<Card.Img variant="top" src="holder.js/100px180" />
<Card.Body>
<Card.Title>{data.weather.description}</Card.Title>
<Card.Text>
Some quick example text to build on the card title and make up the bulk of
the card's content.
</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
);
}
export default (WeatherTile);