【问题标题】:Using an API to create data in a React-Table使用 API 在 React-Table 中创建数据
【发布时间】:2020-09-07 13:53:09
【问题描述】:

我对 React.js 非常陌生,我想在这里使用这个 api:https://covidtracking.com/api/v1/states/current.json 来创建一个表格,其中包含来自美国各州的最新 COVID-19 数据。问题是,当我渲染应用程序时,我似乎无法将数据上传到表中。任何帮助将不胜感激,谢谢。

import React, { useMemo, useState, useEffect } from "react";
import axios from "axios";
import Table from "./Table";
import "./App.css";

function App() {
  
  const [loadingData, setLoadingData] = useState(true);
  const columns = useMemo(() => [
    {
      Header: "State",
      accessor: "show.state",
    },
    {
      Header: "Positive Cases",
      accessor: "show.positive",
    },
    {
      Header: "Recovered Cases",
      accessor: "show.recovered",
      

    },
    {
      Header: "Deaths",
      accessor: "show.death",
    },
    {
      Header: "Total Tested",
      accessor: "show.total",
    }
  
  ]);

  const [data, setData] = useState([]);

  useEffect(() => {
    async function getData() {
      await axios
        (.get("https://covidtracking.com/api/v1/states/current.json")
        .then((response)) => {
      
          console.log(response.data);
          setData(response.data);
          setLoadingData(false);
        });
    }
    if (loadingData) {
      getData();
    }
  }, []);

  return (
    <div className="App">
      {loadingData ? (
      ) : (
        <Table columns={columns} data={data} />
      )}
    </div>
  );
}

export default App;

【问题讨论】:

    标签: reactjs axios react-table


    【解决方案1】:
    import React, { useMemo, useState, useEffect } from "react";
    import axios from "axios";
    import Table from "./Table";
    import "./App.css";
    
    function App() {
      // here you set a state to tell the component it need to wait
      //  until the result is fetched from the api
      const [loadingData, setLoadingData] = useState(true);
      const columns = useMemo(() => [
        {
          Header: "State",
          accessor: "state",
        },
        {
          Header: "Positive Cases",
          accessor: "positive",
        },
        {
          Header: "Recovered Cases",
          accessor: "recovered",
        },
      ]);
    
      const [data, setData] = useState([]);
    
      useEffect(() => {
        async function getData() {
          await axios
            .get("https://covidtracking.com/api/v1/states/current.json")
            .then((response) => {
              // check if the data is populated
              console.log(response.data);
              setData(response.data);
              // you tell it that you had the result
              setLoadingData(false);
            });
        }
        if (loadingData) {
          // if the result is not ready so you make the axios call
          getData();
        }
      }, []);
    
      return (
        <div className="App">
          {/* here you check if the state is loading otherwise if you wioll not call that you will get a blank page because the data is an empty array at the moment of mounting */}
          {loadingData ? (
            <p>Loading Please wait...</p>
          ) : (
            <Table columns={columns} data={data} />
          )}
        </div>
      );
    }
    
    export default App;
    

    【讨论】:

    • 我刚试过,但同样的事情发生了——只是一个空白页,除了标题之外没有任何数据,
    • 粘贴您的表格组件,我将更改它并使用工作组件更新我的响应
    • 它现在可以用一个关于问题的小解释来工作。
    • 表文件是否需要更改?因为我得到的结果与以前完全相同。
    • 一秒钟我在看那个
    猜你喜欢
    • 2021-06-10
    • 2021-01-15
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 2022-06-16
    • 2016-10-04
    • 2019-11-29
    • 1970-01-01
    相关资源
    最近更新 更多