简体   繁体   English

React Native 如何同时获取多个 API?

[英]React Native How to get multiple APIs at the same time?

For my project, I have to get several APIs in my projects, these APIs are linked to the others, ie they have the same data ...对于我的项目,我必须在我的项目中获取多个 API,这些 API 与其他 API 相关联,即它们具有相同的数据......

Here is my code这是我的代码


export default class App extends React.Component {

  constructor(props) {
    super(props);
  }

  componentDidMount() {
    Promise.all([
      getData(),
      getData('?page=2'),
    ])
      .then(([dataSource1, dataSource2]) => {
        this.setState({
          isLoading: false,
          isLoading2: false,
          dataSource1,
          dataSource2,
        });
      })
      .catch((error) => {
        // handle errors
      });
  }

  render() {
    const getData = (subpath = '') => fetch(`https://api.rawg.io/api/games${subpath}`)
      .then(res => res.json())
      .then(result => result.results);
    console.log(getData)
        

  }

I tried with axios but without success ...我试过 axios 但没有成功......

When I remove the comment, it shows me only the second fetch ...当我删除评论时,它只向我显示了第二次提取...

You need two separate fetch calls for each API.每个 API 需要两个单独的fetch调用。 To wait for both to finish, use Promise.all .要等待两者都完成,请使用Promise.all

const getData = (subpath = '') => fetch(`https://api.rawg.io/api/games${subpath}`)
    .then(res => res.json())
    .then(result => result.results);
componentDidMount() {
    Promise.all([
        getData(),
        getData('?page=2'),
    ])
        .then(([dataSource1, dataSource2]) => {
            this.setState({
                isLoading: false,
                isLoading2: false,
                dataSource1,
                dataSource2,
            });
        })
        .catch((error) => {
            // handle errors
        });
}

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

相关问题 如何从多个状态中过滤出数据,从 React Native 中的单独 API 获取数据 - How to filter out data from multiple states getting data from separate APIs in react native 如何同时使用classes和props javascript react native - How to use classes and props at the same time javascript react native 如何在React Native中首次单击时同时更改屏幕和setState - How to change screen and setState at the same time on first click in React Native React Native 同时修改相同的 state - React native modifying the same state at the same time React Native - 如何使用相同的代码在 react-native 的两个不同页面中获得相同的输出? - React Native - How to get same output in two different pages in react-native with same code? 如何在React Native中一次在多个状态下使用过滤功能 - How to use filter function in multiple states at one time in react native 如何在 react-native 中一次/同时运行多个轨道? - How to run multiple tracks at a time / simultaneously in react-native? 如何在javascript中同时获取多个输入? - how to get multiple input at the same time in javascript? 如何使用 react-native-date-picker 获取时间 - How to get time with react-native-date-picker 我如何让本机反应播放多个本地文件 - how do i get react native to play multiple local files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM