I'm creating a web app in React that needs to handle 2 API calls where one is dependent on the other. The first API call will grab data from OpenWeather API - and then the 2nd API call will use that callback data to make a call to Spotify's API.
How can I set up this nested/dependent API call in React? Can I run an ajax call under the success function of the first API call? Or do I need to create a new component that handles the 2nd API, that will somehow get the data from the first API call?
// Making the API call to OpenWeather API:
var CityInfo = React.createClass({
getInitialState: function() {
return {data: {}};
},
loadCityInfo: function(e){
var city = $(e.currentTarget).data('city');
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/weather?q='+city,
method: 'GET',
success: function(result) {
this.setState({data: result});
console.log(result);
}.bind(this)
});
},
render: function() {
return (
<div>
<h2><button onClick={this.loadCityInfo} data-city={this.props.info.name}>{this.props.info.name}</button></h2>
</div>
);
}
});
Full code: https://jsbin.com/lefupo/edit?js,output
successfunction is just another function. You can execute any other JavaScript in it.