第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

React:如何獲取多個(gè) API

React:如何獲取多個(gè) API

HUX布斯 2021-10-29 13:26:35
使用 NHL APi,嘗試制作一個(gè)只顯示每個(gè)玩家信息的 React 應(yīng)用程序。該代碼現(xiàn)在確實(shí)有效(返回一般名單信息和一名特定球員的數(shù)據(jù)),但我想顯示球隊(duì)的所有球員信息。不止一個(gè)。我是使用多個(gè) API 的新手,大約有 47 個(gè)玩家。我真的必須單獨(dú)獲取每個(gè)玩家,進(jìn)行 47 個(gè) API 調(diào)用嗎?或者,還有更好的方法?想知道我是否能夠獲取名冊(cè) API 一次,然后以某種方式從那里顯示所有單個(gè)玩家的信息。任何幫助將不勝感激!import React, { Component } from "react";import ReactDOM from "react-dom";import "./styles.css";class App extends Component {  state = {    loading: true,    roster: null,    spezza: null  };  // Always fetch here  async componentDidMount() {    // Roster    const api = "https://statsapi.web.nhl.com/api/v1/teams/10/roster";    const response = await fetch(api);    const data = await response.json();    console.log(data.roster[0]);    // Spezza    const apiSpezza = "https://statsapi.web.nhl.com/api/v1/people/8469455";    const responseSpezza = await fetch(apiSpezza);    const dataSpezza = await responseSpezza.json();    console.log(dataSpezza.people[0]);    // When the component mounts, set the new state values based on the API    this.setState({      loading: false,      roster: data.roster[0],      spezza: dataSpezza.people[0]    });  }  render() {    return (      <div className="App">        {/* If loading = true - meaning we can't fetch the API, then display loading.        If loading value = false, meaning we fetched it, display the values from the API */}        {this.state.loading ? (          <div>loading...</div>        ) : (          <>            <h1>Roster:</h1>            <p>Jersey Number: {this.state.roster.jerseyNumber}</p>            <p>Full Name: {this.state.roster.person.fullName}</p>            <h1>Spezza:</h1>            <p>Jersey Number: {this.state.spezza.primaryNumber}</p>            <p>Age: {this.state.spezza.currentAge}</p>            <p>Height: {this.state.spezza.height}</p>            <p>Weight: {this.state.spezza.weight} lbs</p>            <p>Nationalty: {this.state.spezza.nationality}</p>          </>        )}      </div>    );  }}const rootElement = document.getElementById("root");ReactDOM.render(<App />, rootElement);
查看完整描述

3 回答

?
慕娘9325324

TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊

Promise.all([

    fetch(apiSpezza),

    fetch(api),

  ]).then(([data1, data2]) => {

cosnole.log(data1,data2);

})

我會(huì)推薦使用 localhost 調(diào)用 API,不要直接傳遞 API URL。


查看完整回答
反對(duì) 回復(fù) 2021-10-29
  • 3 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)