简体   繁体   English

React.js 中的嵌套 API 调用

[英]Nested API calls in React.js

I'm creating a web app in React that needs to handle 2 API calls where one is dependent on the other.我正在 React 中创建一个 web 应用程序,它需要处理 2 个 API 调用,其中一个依赖于另一个。 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.第一个 API 调用将从 OpenWeather API 获取数据 - 然后第二个 API 调用将使用该回调数据调用 Spotify 的 API。

How can I set up this nested/dependent API call in React?如何在 React 中设置此嵌套/依赖 API 调用? Can I run an ajax call under the success function of the first API call?我可以在第一个 API 调用的成功函数下运行 ajax 调用吗? 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?或者我是否需要创建一个处理第二个 API 的新组件,它会以某种方式从第一个 API 调用中获取数据?

    // 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完整代码: https : //jsbin.com/lefupo/edit?js,output

I would recommend you follow flux pattern for making your requests especially.. however with what you have you can make a chained request我建议你遵循通量模式来提出你的请求,特别是......但是你可以用你所拥有的来提出一个链式请求

var CityInfo = React.createClass({
    loadCityInfo: function(e){
        var city = $(e.currentTarget).data('city');
        $.ajax({
            url: 'http://api.openweathermap.org/data/2.5/weather?q='+city+"&APPID=3c8e84ea9c6f7938384d3c3980033d80",
            method: 'GET',
            success: function(result) {
                this.setState({data: result});
                console.log(result);
                // make request here
            }.bind(this)
        });
    }
});

now that aside the way you can do this with flux pattern is have an actions file that does the requests..现在,除了您可以使用通量模式执行此操作的方式之外,还有一个执行请求的操作文件..

module.exports = {
    loadWeather: function(city){
        return $.ajax({
            url: 'http://api.openweathermap.org/data/2.5/weather?q='+city+"&APPID=3c8e84ea9c6f7938384d3c3980033d80",
            method: 'GET'
        });
    },
    loadSpotify: function(params){
        //do request here
    }
}

then in your loadcity function you can just do this然后在您的 loadcity 函数中,您可以执行此操作

loadCityInfo: function(e){
    var city = $(e.currentTarget).data('city');
    actions.loadWeather(city).done(function(res){
        //set your state
        actions.loadSpotify(params)
    }).fail(funciton(res){
        //handle fail
    });
}

Can I run an ajax call under the success function of the first API call?我可以在第一个 API 调用的成功函数下运行 ajax 调用吗?

Of course.当然。 The success function is just another function. success函数只是另一个函数。 You can execute any other JavaScript in it.您可以在其中执行任何其他 JavaScript。

However, it would be cleaner to utilize the fact the $.ajax returns a promise.然而,利用$.ajax返回承诺的事实会更干净。 So you could write this instead:所以你可以这样写:

getWeatherData(city) // contains the Ajax call to OpenWeather
.then(getSpotifyData) // contains call to Spotify, gets passed result from getWeatherData
.then(function(result) {
  // update state
});

See Deferreds for more info.有关更多信息,请参阅延迟

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM