最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why use `useTable` over `ReactTable` when using react-table - Stack Overflow

programmeradmin2浏览0评论

On their npm page, the example shows the usage of <ReactTable> component:

import ReactTable from 'react-table'
...
render() {
  return (
    <ReactTable
      data={data}
      columns={columns}
    />
  )
}

However, on their API Docs and examples, they all use useTable.

import { useTable } from 'react-table';

function Table({ columns, data }) {
  // Use the state and functions returned from useTable to build your UI
  const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    rows,
    prepareRow,
  } = useTable({
    columns,
    data,
  })

  // Render the UI for your table
  return (
    <table {...getTableProps()}>
      <thead>
        {headerGroups.map(headerGroup => (
          <tr {...headerGroup.getHeaderGroupProps()}>
            {headerGroup.headers.map(column => (
              <th {...column.getHeaderProps()}>{column.render('Header')}</th>
            ))}
          </tr>
        ))}
      </thead>
      <tbody {...getTableBodyProps()}>
        {rows.map(
          (row, i) => {
            prepareRow(row);
            return (
              <tr {...row.getRowProps()}>
                {row.cells.map(cell => {
                  return <td {...cell.getCellProps()}>{cell.render('Cell')}</td>
                })}
              </tr>
            )}
        )}
      </tbody>
    </table>
  )
}

...

render () {
  return (
    <Table columns={columns} data={data} />
  )
}

So, my question is: Why would someone use hooks(useTable, useFilters, and etc...) and make Table component when he/she can just use a that's already provided. I'm pretty sure they didn't forget updating their npm page's example... or did they?

On their npm page, the example shows the usage of <ReactTable> component:

import ReactTable from 'react-table'
...
render() {
  return (
    <ReactTable
      data={data}
      columns={columns}
    />
  )
}

However, on their API Docs and examples, they all use useTable.

import { useTable } from 'react-table';

function Table({ columns, data }) {
  // Use the state and functions returned from useTable to build your UI
  const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    rows,
    prepareRow,
  } = useTable({
    columns,
    data,
  })

  // Render the UI for your table
  return (
    <table {...getTableProps()}>
      <thead>
        {headerGroups.map(headerGroup => (
          <tr {...headerGroup.getHeaderGroupProps()}>
            {headerGroup.headers.map(column => (
              <th {...column.getHeaderProps()}>{column.render('Header')}</th>
            ))}
          </tr>
        ))}
      </thead>
      <tbody {...getTableBodyProps()}>
        {rows.map(
          (row, i) => {
            prepareRow(row);
            return (
              <tr {...row.getRowProps()}>
                {row.cells.map(cell => {
                  return <td {...cell.getCellProps()}>{cell.render('Cell')}</td>
                })}
              </tr>
            )}
        )}
      </tbody>
    </table>
  )
}

...

render () {
  return (
    <Table columns={columns} data={data} />
  )
}

So, my question is: Why would someone use hooks(useTable, useFilters, and etc...) and make Table component when he/she can just use a that's already provided. I'm pretty sure they didn't forget updating their npm page's example... or did they?

Share Improve this question edited Mar 9, 2020 at 11:14 Ben Smith 20.2k6 gold badges73 silver badges96 bronze badges asked Nov 19, 2019 at 18:49 AndyAndy 1981 gold badge1 silver badge7 bronze badges 2
  • 1 The npm page says "This documentation is for version 6 of react-table." So the github link on the npm page is not correct. You will get version 6 when you do yarn add react-table because the version 7 is not released yet. – HMR Commented Nov 19, 2019 at 20:37
  • react-table v7 is not yet a release candidate which is why they haven't yet fully advertised it in npm. At the bottom of the NPM page you can see they are asking for sponsorship to get it into shape for release. I've added an answer as to why you would use it instead of ReactTable. – Ben Smith Commented Nov 27, 2019 at 17:09
Add a comment  | 

1 Answer 1

Reset to default 15

react-table is a "headless" UI library which means that it provides only the backend table functionality, and it requires you to implement the rendering of the table with your own React components.

This means that you can apply the backend table code to whichever style of table you want (e.g. Bootstrap, Material UI, vanilla HTML etc.) and you get precise control of where to hook-up this library to the UI.

Most table libraries (including react-table pre v7!) have predefined functionality and behaviours which might not be suitable for you in certain cases; however when using react-table >=v7 you would be able to cater for these situations by implementing your own UI code and then hooking it up to your react-table hook code.

发布评论

评论列表(0)

  1. 暂无评论