Several requests on componentDidMount

后端 未结 1 1116
执念已碎
执念已碎 2021-02-06 16:37

I have a React app with one parent component and three child components. In parent component I have state containing data and pass those data in props to child components. I hav

相关标签:
1条回答
  • 2021-02-06 16:46

    Something like this could work. The ajax calls are psuedo-code. I assume you are using some ajax api libarary. - in this example, I use superagent (w/o its additional promise lib, instead I just use the es6 promise). I use map with Promise.all - basically, we wait till all ajax requests get returned.. in the "then" I update the state with the results. Once the promise.all is resolve, it passes on an array containing each of the requests, in the order you make the request. In "ajaxApi" - those are the api calls. I hope this helps.

    NOTE: I am assuming es6 here, thus my usage of promise.all and some es6 shorthan. If you are not using es6, I apologize. Let me know, and I can show a non es6 solution.

    var Parent = React.createClass({
        getDefaultProps: function() {
            return {
              ajaxApi: ['foo1','foo2','foo3']
            };
        },
        getInitialState: function(){
            return ( {
                data1: [],
                data2: [],
                data3: []
            });
        },
        componentDidMount: function() {
          Promise.all(this.props.ajaxApi
            .map(a => {
                return new Promise((resolve, reject) => {
                    //using superagent here (w/o its promise api), "import request as 'superagent'. You'd import this at the top of your file.
                    request.get(a)
                      .end((error, response) => {
                        if (error) {
                          return resolve(response)
                        } else {
                          resolve()
                        }
                   })
                })
            )
            .then(v => {
                this.setState({
                  data1: v[0],
                  data2: v[1],
                  data3: v[2]
                })
            })
            .catch(() => {
                console.error("Error in data retrieval")
            })
        },
        render: function(){
            return (
                <div>
                    <Child1 data={this.state.data1} />
                    <Child2 data={this.state.data2} />
                    <Child3 data={this.state.data3} />
                </div>
            )
        }
    })
    

    // here is an Axios version w/o es6. I am making some assumptions here. I hope you can adapt to your own needs.

    var Parent = React.createClass({
        // these are your api calls
        getDefaultProps: function() {
            return {
              ajaxApi: ['api/call/1','api/call/2','api/call/3']
            };
        },
        getInitialState: function(){
            return ( {
                data1: [],
                data2: [],
                data3: []
            });
        },
    
        fetchApiCallData: function(api) {
            return axios.get(api);
        },
    
        componentDidMount: function() {
           axios.all(this.props.ajaxApi.map(function(api) {
               fetchApiCallData(api)
           })).then(axios.spread(function(req1, req2, req3) {
            // requests complete
               this.setState({
                  data1: req1,
                  data2: req2,
                  data3: req3
                })
           }));
        },
        render: function(){
            return (
                <div>
                    <Child1 data={this.state.data1} />
                    <Child2 data={this.state.data2} />
                    <Child3 data={this.state.data3} />
                </div>
            )
        }
       })
    
    0 讨论(0)
提交回复
热议问题