【问题标题】:Using React-Hooks/Axios to fetch data and display in a table使用 React-Hooks/Axios 获取数据并显示在表格中
【发布时间】:2019-11-15 16:09:57
【问题描述】:

我有这个 React 设置,我定义了一个名为 ApiTable 的钩子并有一个 renderTable 方法。我要做的是从 api 端点 https://jsonplaceholder.typicode.com/users 获取数据并将其返回到具有适当类别的表中。

现在它正在将左侧的所有列都揉成一团,如图所示。目前,数据未显示并压缩到左侧。我很确定我的表格数据设置错误。

另外,我不确定 axios 请求是否应该在 useEffect 中。

https://imgur.com/a/Up4a56v

const ApiTable = () => {

  const url = 'https://jsonplaceholder.typicode.com/users';

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

  useEffect(() => {

    setData([data]);

    axios.get(url)

    .then(json => console.log(json))

  }, []);

  const renderTable = () => {

      return data.map((user) => {

        const { name, email, address, company } = user;

        return (
          <div>
           <thead>
               <tr>
                 <th>Name</th>
                 <th>Email</th>
                 <th>Address</th>
                 <th>Company</th>
               </tr>
          </thead>
          <tbody>
          <tr>
              <td>name</td>
              <td>email</td>
              <td>address</td>
              <td>company</td>
          </tr>
          </tbody>
          </div>
        )
      })
    }


      return (
        <div>
            <h1 id='title'>API Table</h1>
            <Table id='users'>
              {renderTable()}
            </Table>
         </div>
      )

};

【问题讨论】:

    标签: javascript json axios react-hooks


    【解决方案1】:

    您正在正确获取数据,但将数据设置为错误状态。

    此外,当您迭代 data 数组时,您每次都在打印 table head,这是错误的,并且从您的 data 数组中,addresscompany 是对象,因此您无法直接打印该对象。

    你需要这样做,

    const App = () => {
      const url = 'https://jsonplaceholder.typicode.com/users'
    
      const [data, setData] = useState([])
    
      useEffect(() => {
        axios.get(url).then(json => setData(json.data))
      }, [])
    
      const renderTable = () => {
        return data.map(user => {
          return (
            <tr>
              <td>{user.name}</td>
              <td>{user.email}</td>
              <td>{user.address.street}</td> //only street name shown, if you need to show complete address then you need to iterate over `user.address` object
              <td>{user.company.name}</td> //only company name shown, if you need to show complete company name then you need to iterate over `user.name` object
            </tr>
          )
        })
      }
    
      return (
        <div>
          <h1 id="title">API Table</h1>
          <table id="users"> //Your Table in post changed to table to make it work
            <thead>
              <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Address</th>
                <th>Company</th>
              </tr>
            </thead>
            <tbody>{renderTable()}</tbody>
          </table>
        </div>
      )
    }
    

    Demo

    【讨论】:

    • 欣赏答案,完全有道理。对于 axios 部分,(json =&gt; setData(json.data) 是否需要 json 部分? axios 不会自动返回 json 吗?还是我们需要指定这个?
    • axios.get(url).then(json =&gt; setData(json.data)) ,在这个 json 只是一个名字,因为你可以拥有任何你想要的东西。 Axios 响应存储在此变量中,然后使用setData(json.data) 将其存储在状态中。如果您将json 更改为response,那么您应该写这个axios.get(url).then(response =&gt; setData(response.data))。这里我们从response.data 设置数据,因为我们在response 对象中以data 为键获取数据。
    • 啊,明白了,只是一个你可以命名的变量,感谢清晰
    猜你喜欢
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多