如何使用axios发出API请求?

时间:2019-04-08 22:09:38

标签: reactjs api axios setstate

我是反应的初学者,并且遇到以下问题:

我正在使用包含60个以上字符的API,我的问题是我想确保API的所有字符都呈现出来,而无需我手动添加每个数字。({this.state.people [0] .name},...,{this.state.people [n] .name})。

此问题也使我进行了两次API调用,因为API每次调用仅给出10个结果。接下来是第2页。

我使用?= page2创建了一个新调用,但这是一种非常繁琐的方法,因为我必须添加10个不同的API调用才能添加所有字符。 我考虑过要进行for循环,但我真的不知道该如何处理。

class Api extends Component {

state ={
    people:[
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    ],

    people2:[
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    {name:'Title'},
    ]
}

componentDidMount() {
  axios.get('https://swapi.co/api/people/')

  .then(starwars => {
    this.setState({
        people: starwars.data.results
    })
    console.log(starwars.data.results)

  })
axios.get('https://swapi.co/api/people/?page=2')
  .then(starwars2 => {
    this.setState({
        people2: starwars2.data.results
    })
    console.log(starwars2.data.results)

  })
  .catch(error => {
    console.log(error);
  });
}

  render(){
    return ( 

        <div>
        <Cards title={this.state.people[0].name} />
        <Cards title={this.state.people[1].name} />
        <Cards title={this.state.people[2].name} />
        <Cards title={this.state.people[3].name} />
        <Cards title={this.state.people[4].name} />
        <Cards title={this.state.people[5].name} />
        <Cards title={this.state.people[6].name} />
        <Cards title={this.state.people[7].name} />
        <Cards title={this.state.people[8].name} />
        <Cards title={this.state.people[9].name}  />
        <Cards title={this.state.people2[0].name}  />
        <Cards title={this.state.people2[1].name}  />
        <Cards title={this.state.people2[2].name}  />
        <Cards title={this.state.people2[3].name}  />
        <Cards title={this.state.people2[4].name}  />
        <Cards title={this.state.people2[5].name}  />
        <Cards title={this.state.people2[6].name}  />
        <Cards title={this.state.people2[7].name}  />
        <Cards title={this.state.people2[8].name}  />
        <Cards title={this.state.people2[9].name}  />

</div>)
  }
}
export default Api;


在一次调用中对60个字符进行API调用。

1 个答案:

答案 0 :(得分:3)

正如您所说,您可以通过编程循环遍历所有人员。为此,您需要知道有多少页。请求https://swapi.co/api/people/返回count作为响应的一部分。假设这是总人数,则可以使用它来计算需要查询的页面数。看起来像这样:

注意:我为此使用了Node,只需添加必要的React位即可。

const axios = require("axios");
// first page
axios("https://swapi.co/api/people/")
    .then(starwars => {
        return starwars.data.count;
    })
    .then(count => {
        // exclude the first request
        const numberOfPagesLeft = Math.ceil((count - 1) / 10);
        let promises = [];
        // start at 2 as you already queried the first page
        for (let i = 2; i <= numberOfPagesLeft; i++) {
            promises.push(axios(`https://swapi.co/api/people?page=${i}`));
        }
        return Promise.all(promises);
    })
    .then(response => {
        // collect all results into one array
        const result = response.reduce((acc, data) => [...acc, ...data.data.results], []);
        return result;
        //result should contain the remaining records - pages 2 through n.
    })
    .catch(error => console.log("Properly handle your exception here"));

一如既往,请注意Promise.all的{​​{3}}行为