简体   繁体   English

使用React-Hooks / Axios来获取数据并显示在表格中

[英]Using React-Hooks/Axios to fetch data and display in a table

I have this React setup, I defined a hook called ApiTable and have a renderTable method. 我有这个React设置,我定义了一个名为ApiTable的钩子,并有一个renderTable方法。 What I'm trying to do is get the data from the api endpoint, https://jsonplaceholder.typicode.com/users and return it into a table with the appropriate category. 我正在尝试从api端点https://jsonplaceholder.typicode.com/users获取数据,并将其返回到具有适当类别的表中。

Right now it's scrunching up all the columns onto the left side as seen here. 现在,它会将所有列都压缩到左侧,如此处所示。 Currently, the data isn't showing up and is compacted to the left side. 当前,数据未显示,并被压缩到左侧。 I'm pretty sure I have the table data setup wrong. 我很确定我的表数据设置错误。

Also, I'm not sure if the axios request is supposed to inside the useEffect or not. 另外,我不确定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> ) }; 

You are fetching data correctly, but setting data to state wrongly. 您正在正确获取数据,但是将数据设置为错误状态。

Also when you iterating your data array, you are printing table head each time which is wrong and from your data array address and company are object so you cant direct print the object. 同样,当您迭代data数组时,每次都会打印table head ,这是错误的,并且您的data数组addresscompany都是对象,因此您无法直接打印对象。

You need to do this, 你需要这样做

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 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM