reactjs - 如何 react "fetch"

标签 reactjs

我尝试从两个地方读取数据:“/api/testimonial”和“/api/information” 对于第一个(/api/testimonial),我编写了这段代码并且它可以正常工作

    export default class Testimonials extends React.Component {
      constructor(){
        super();
            this.state ={
              customers:[]
            }
          }
          componentDidMount(){
            fetch('/api/testimonial')
            .then(res => res.json())
            .then(customers => this.setState({customers},() => console.log('customers fetched..', 
    customers)));
          }
      render() {
        return (

但我还需要从“/api/information”读取一些信息。我怎样才能同时获取这两个数据?

最佳答案

看看你是否可以利用 promise ,

Promise.all([
  fetch("http://localhost:3000/API1"),
  fetch("http://localhost:3000/API2"),
]).then(([results1, results2]) => {
  //Process results 
}).catch((err) => {
  console.log(err);
});

关于reactjs - 如何 react "fetch",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61187342/

相关文章:

reactjs - 在 React/Redux 应用程序中存储 RTCPeerConnection 对象的最佳位置在哪里?

javascript - 使用 useSelector 强制进行一次重新渲染

javascript - 如何在 React 中修改具有子级多个级别的父 DOM 上的状态

javascript - 具有未定义属性的 React 选项卡

reactjs - Antd:如何更改 Antd Form.Item 标签文本颜色?

javascript - 当我只更改状态且不使用任何 CSS 时,为什么我的 React 应用程序菜单会打开?

javascript - 如何在持久的 NextJS React Audio Player 组件中捕获 Mobx 可观察更新?

javascript - 动态创建回调

ios - 我在我的 native 包中找不到index.ios.js和index.android.js,而是存在index.js

javascript - React/React-Native 订阅 onChange 的最高效方式