React Table v7

7.8.0 · maintenance · verified Sun Apr 19

React Table v7 is a set of headless hooks for building highly customizable and performant data tables in React applications. As of version 7.8.0, it provides core functionalities like sorting, filtering, pagination, row selection, and expansion through a flexible plugin system. Its "headless" nature means it provides the logic and state management, leaving all UI rendering and styling entirely up to the developer, offering maximum customization. While `react-table` v7 remains stable and widely used, active development and new features have shifted to the `@tanstack/react-table` package (v8 and beyond). Developers starting new projects are generally advised to use the newer TanStack Table versions, which offer a similar headless approach across various frameworks. This package (v7) is now in a maintenance phase.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic data table using `react-table` v7, incorporating sorting and pagination functionalities with custom UI rendering.

import React from 'react';
import { useTable, useSortBy, usePagination, Column } from 'react-table';

interface MyData {
  id: number;
  firstName: string;
  lastName: string;
  age: number;
  visits: number;
  status: string;
}

function MyTable() {
  const data: MyData[] = React.useMemo(
    () => [
      { id: 1, firstName: 'Tanner', lastName: 'Linsley', age: 30, visits: 100, status: 'complicated' },
      { id: 2, firstName: 'Kevin', lastName: 'Van Cott', age: 25, visits: 40, status: 'single' },
      { id: 3, firstName: 'Sarah', lastName: 'Doe', age: 35, visits: 20, status: 'married' },
    ],
    []
  );

  const columns: Column<MyData>[] = React.useMemo(
    () => [
      { Header: 'ID', accessor: 'id' },
      { Header: 'First Name', accessor: 'firstName' },
      { Header: 'Last Name', accessor: 'lastName' },
      { Header: 'Age', accessor: 'age' },
      { Header: 'Visits', accessor: 'visits' },
      { Header: 'Status', accessor: 'status' }
    ],
    []
  );

  const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    page,
    nextPage,
    previousPage,
    canNextPage,
    canPreviousPage,
    pageOptions,
    state: { pageIndex, pageSize },
    prepareRow,
  } = useTable<
    MyData
  >(
    {
      columns,
      data,
      initialState: { pageIndex: 0, pageSize: 2 },
    },
    useSortBy,
    usePagination
  );

  return (
    <div>
      <table {...getTableProps()} style={{ border: '1px solid black', width: '100%' }}>
        <thead>
          {headerGroups.map(headerGroup => (
            <tr {...headerGroup.getHeaderGroupProps()}>
              {headerGroup.headers.map(column => (
                <th
                  {...column.getHeaderProps(column.getSortByToggleProps())}
                  style={{ borderBottom: 'solid 3px red', background: 'aliceblue', color: 'black', fontWeight: 'bold' }}
                >
                  {column.render('Header')}
                  <span>
                    {column.isSorted
                      ? column.isSortedDesc
                        ? ' 🔽'
                        : ' 🔼'
                      : ''}
                  </span>
                </th>
              ))}
            </tr>
          ))}
        </thead>
        <tbody {...getTableBodyProps()}>
          {page.map(row => {
            prepareRow(row);
            return (
              <tr {...row.getRowProps()}>
                {row.cells.map(cell => (
                  <td
                    {...cell.getCellProps()}
                    style={{
                      padding: '10px',
                      border: 'solid 1px gray',
                      background: 'papayawhip',
                    }}
                  >
                    {cell.render('Cell')}
                  </td>
                ))}
              </tr>
            );
          })}
        </tbody>
      </table>
      <div style={{ padding: '10px' }}>
        <button onClick={() => previousPage()} disabled={!canPreviousPage}>
          Previous Page
        </button>
        <button onClick={() => nextPage()} disabled={!canNextPage}>
          Next Page
        </button>
        <span>
          Page{' '}
          <strong>
            {pageIndex + 1} of {pageOptions.length}
          </strong>{' '}
        </span>
      </div>
    </div>
  );
}

export default MyTable;

view raw JSON →