首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在React中处理多个GET请求时处理第二个响应

在React中处理多个GET请求时处理第二个响应
EN

Stack Overflow用户
提问于 2018-11-06 10:29:09
回答 1查看 399关注 0票数 1

我已经设法将第一个响应传递到组件的状态名称数据(this.state.data)中。但是当我尝试从另一个API对data1进行第二次请求时,它没有出现。从签入dev工具和第一个我可以使用数据的响应来看,两个响应都很好。

代码语言:javascript
运行
复制
class WeatherApp extends React.Component {
constructor(props) {
  super(props);
  this.state = {
    error: null,
    isLoaded: false,
    data: [],
    data1: []
  };
}

componentDidMount() {
  Promise.all([
      fetch("weatherAPI.php"),
      fetch("weatherAPIToday.php")
    ]).then(([res, res1]) => res.json())
    .then((result, result1) => {
        this.setState({
          isLoaded: true,
          data: result,
          data1: result1
        }, console.log(result));
      },

      (error) => {
        this.setState({
          isLoaded: true,
          error
        });
      }
    );
}
EN

Stack Overflow用户

发布于 2018-11-06 10:48:42

因为res和res1都返回一个promise,所以您所要做的就是将它们都包装在一个Promise#all中

代码语言:javascript
运行
复制
Promise.all([
    fetch("weatherAPI.php"),
    fetch("weatherAPIToday.php")
]).then(([res, res1]) => Promise.all(res.json(), res1.json()))
.then((result, result1) => {
    this.setState({
        isLoaded: true,
        data: result,
        data1: result1
    }, console.log(result));
})
.catch(error => {
    this.setState({ isLoaded: true, error });
});
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53164999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档