# React Table
Hooks for building **lightweight, fast and extendable datagrids** for React
## Features
- Lightweight
- Headless (100% customizable, Bring-your-own-UI)
- Client-side & Server-side pagination support
- Sorting (Multi and Stable)
- Filters
- Pivoting & Aggregation
- Fully controllable
- Extensible via hooks
- "Why I wrote React Table and the problems it has solved for Nozzle.io" by Tanner Linsley
## [See Examples](#examples)
## Versions
- This documentation is for version 7.
- [View the Changelog](https://github.com/tannerlinsley/react-table/blob/master/CHANGELOG.md)
- Previous versions:
- [6.x.x Readme](https://github.com/tannerlinsley/react-table/tree/v6/)
- [5.x.x Readme](https://github.com/tannerlinsley/react-table/blob/ad7d31cd3978eb45da7c6194dbab93c1e9a8594d/README.md)
## Sponsors
**React Table v7** is being built and maintained by me, @tannerlinsley and I am always in need of more Patreon support to keep this project afloat. If you would like to contribute to my Patreon goal for v7 and beyond, [visit my Patreon and help me out!](https://patreon.com/tannerlinsley).
# Documentation
- [Installation](#installation)
- [Concepts](#concepts)
- [Setup](#setup)
- [Contributing](#contributing)
# Installation
Install React Table as a dependency using `npm` or `yarn`
```bash
# NPM
$ npm install react-table
# Yarn
$ yarn add react-table
```
To import React Table:
```js
import {
useTable,
useGroupBy,
useFilters,
useSortBy,
useExpanded,
usePagination,
...
} from 'react-table'
```
# Examples
- [Basic](https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/basic)
- [Sorting - Client Side](https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/sorting-client-side)
- [Filtering - Client Side](https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/filtering-client-side)
- [Grouping - Client Side](https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/grouping-client-side)
# Concepts
## React Table is a "headless" UI library
React Table is a headless utility, which means out of the box, it doesn't render or supply any actual UI elements. You are in charge of utilizing the state and callbacks of the hooks provided by this library to render your own table markup. [Read this article to understand why React Table is built this way.](https://medium.com/merrickchristensen/headless-user-interface-components-565b0c0f2e18). If you don't want to, then here's a quick rundown anyway:
- Separation of Concerns - Not that superficial kind you read about all the time. The real kind. React Table as a library honestly has no business being in charge of your UI. The look, feel, and overall experience of your table is what makes your app or product great. The less React Table gets in the way of that, the better!
- Maintenance - By removing the massive (and seemingly endless) API surface area required to support every UI use-case, React Table can remain small, easy-to-use and simple to update/maintain.
- Extensibility - UI presents countless edge cases for a library simply because it's a creative medium, and one where every developer does things differently. By not dictating UI concerns, React Table empowers the developer to design and extend the UI based on their unique use-case.
## The React Table API
At the heart of every React Table is a table `instance` object. This object contains everything needed to build a table and interact with it's state. This includes, but is not limited to:
- Columns
- Materialized Data
- Sorting
- Filtering
- Grouping
- Pagination
- Expanded State
- Any functionality provided by custom plugin hooks, too!
## Using Hooks for configuration, state and lifecycle
React Table uses React Hooks both internally and externally for 100% of its configuration and lifecycle management. Naturally, this is what allows React Table to be headless and lightweight while still having a concise and simple API.
React Table is essentially a compatible collection of **custom React hooks**:
- The primary React Table hook
- [`useTable`](#usetable)
- Plugin Hooks
- Core Plugin Hooks
- [`useTableState`](#useTableState)
- [`useGroupBy`](#useGroupBy)
- [`useFilters`](#useFilters)
- [`useSortBy`](#useSortBy)
- [`useExpanded`](#useExpanded)
- [`usePagination`](#usePagination)
- [`useTokenPagination`](#useTokenPagination)
- Layout Plugin Hooks
- [`useFlexLayout`](#useFlexLayout)
- [`useAbsoluteLayout`](#useAbsoluteLayout) (coming soon!)
- Custom Plugin Hooks
- Get your custom plugin hook listed here!
### Hook Usage
`useTable` is the **primary** hook used to build a React Table. It serves as the starting point for **every option and every plugin hook** that React Table supports. The options passed into `useTable` are supplied to every plugin hook after it in the order they are supplied, eventually resulting a final `instance` object that you can use to build your table UI and interact with the table's state.
```js
const instance = useTable(
{
data: [...],
columns: [...],
},
useGroupBy,
useFilters,
useSortBy,
useExpanded,
usePagination
)
```
### The stages of a React Table
1. `useTable` is called. A table instance is created.
1. The `instance.state` is resolved from either a custom user state or an automatically generated one.
1. A collection of plugin points is created at `instance.hooks`.
1. Each plugin is given the opportunity to add hooks to `instance.hook`.
1. As the `useTable` logic proceeds to run, each plugin hook type is used at a specific point in time with each individual hook function being executed the order it was registered.
1. The final instance object is returned from `useTable`, which the developer then uses to construct their table.
This multi-stage process is the secret sauce that allows React Table plugin hooks to work together and compose nicely, while not stepping on each others toes.
To dive deeper into plugins, see [Plugins](TODO) and the [Plugin Guide](TODO)
### Plugin Hook Order & Consistency
The order and usage of plugin hooks must follow [The Laws of Hooks](TODO), just like any other custom hook. They must always be unconditionally called in the same order.
> **NOTE: In the event that you want to programmatically enable or disable plugin hooks, most of them provide options to disable their functionality, eg. `options.disableSorting`**
### Option Memoization
React Table relies on memoization to determine when state and side effects should update or be calculated. This means that every option you pass to `useTable` should be memoized either via `React.useMemo` (for objects) or `React.useCallback` (for functions).
# API
## `useTable`
- Required
`useTable` is the root hook for React Table. To use it, pass it with an options object with at least a `columns` and `rows` value, followed by any React Table compatible hooks you want to use.
### Table Options
The following options are supported via the main options object passed to `useTable(options)`
- `columns: Array`
- Required
- Must be **memoized**
- The core columns configuration object for the entire table.
- Supports nested `columns` arrays via the `column.children` key
- `data: Array`
- Required
- Must be **memoized**
- The data array that you want to display on the table.
- `state: TableStateTuple[stateObject, stateUpdater]`
- Optional
- Must be **memoized** table state tuple. See [`useTableState`](#usetablestate) for more information.
- The state/updater pair for the table instance. You would want to override this if you plan on controlling or hoisting table state into your own code.
- Defaults to using an internal `useTableState()` instance if not defined.
- See [Controlling and Hoisting Table State](#controlling-and-hoisting-table-state)
- `defaultColumn: Object`
- Optional
- Defaults to `{}`
- The default column object for every column passed to React Table.
- Column-specific properties will override the properties in this object, eg. `{ ...defaultColumn, ...userColumn }`
- This is particularly useful for adding global column properties. For instance, when using the `useFilters` plugin hook, add a default `Filter` renderer for every column, eg.`{ Filter: MyDefaultFilterComponent }`
- `debug: Bool`
- Optional
- A flag to turn on debug mode.
- Defaults to `false`
### `column` Options
The following options are supported on any column object you can pass to `columns`.
- `accessor: String | Function`
- **Required**
- This string/function is used to build the data model for your column.
- The data returned by an accessor should be **primitive** and sortable.
- If a string is passed, the column's value will be looked up on the original row via that key, eg. If your column's accessor is `firstName` then its value would be read from `row['firstName']`. You can also specify deeply nested values with accessors like `info.hobbies` or even `address[0].street`
- If a function is passed, the column's value will be looked up on the original row using this accessor function, eg. If your column's accessor is `row => row.firstName`, then its value would be determined by passing the row to this function and using the resulting value.
- `id: String`
- **Required if `accessor` is a function**
- This is the unique ID for the column. It is used by reference in things like sorting, grouping, filtering etc.
- If a **string** accessor is used, it defaults as the column ID, but can be overridden if necessary.
- `columns: Array`
- Optional
- A nested array of columns.
- If defined, the column will act as a header group. Columns can be recursively nested as much as needed.
- `show: Boolean | Function`
- Optional
- Defaults to `true`
- If set to `false`, the column will be hidden.
- If set to a `function`, it will be called with the current table instance and can then return `true` or `false`.
- The data model for hidden columns is still calculated including sorting, filters, and grouping.
- `Header: String | Function | React.Component => JSX`
- Optional
- Defaults to `({ id }) => id`
- Receives the table instance and column model as props
- Must either be a **string or return valid JSX**
- If a function/component is passed, it will be used for formatting the header value, eg. You can use a `Header` function to dynamically format the header using any table or column state.
- `Cell: Function | React.Component => JSX`
- Optional
- Defaults to `({ value }) => value`
- Receives the table instance and cell model as props
- Must return valid JSX
- This function (or component) is primarily used for formatting the column value, eg. If your column accessor returns a date object, you can use a `Cell` function to format that date to a readable format.
### `Instance` Properties
The following properties are available on the table instance returned from `useTable`
- `headerGroups: Array`
- An array of normalized header groups, each containing a flattened array of final column objects for that row.
- See [Header Group Properties](#headergroup-properties) for more information
- `columns: Array`
- A **flat** array of all final column objects computed from the original columns configuration option.
- See [Column Properties](#column-properties) for more information
- `headers[] Array`
- A **nested** array of final column objects, similar in structure to the original columns configuration option.
- See [Column Properties](#column-properties) for more information
- `rows: Array`
- An array of **materialized row objects** from the original `data` array and `columns` passed into the table options
- See [Row Properties](#row-properties) for more information
- `getTableProps: Function(?props)`
- **Required**
- This function is used to resolve any props needed for your table wrapper.
- Custom props may be passed. **NOTE: Custom props will override built-in table props, so be careful!**
- `prepareRow: Function(Row)`
- **Required**
- This function is responsible for lazily preparing a row for rendering. Any row that you intend to render in your table needs to be passed to this function **before every render**.
- **Why?** Since table data could potentially be very large, it can become very expensive to compute all of the necessary state for every row to be rendered regardless if it actually is rendered or not (for example if you are paginating or virtualizing the rows, you may only have a few rows visible at any given moment). This function allows only the rows you intend to display to be computed and prepped with the correct state.
### `HeaderGroup` Properties
The following additional properties are available on every `headerGroup` object returned by the table instance.
- `headers: Array`
- **Required**
- The columns in this header group.
- `getHeaderGroupProps: Function(?props)`
- **Required**
- This function is used to resolve any props needed for this header group's row.
- You can use the `getHeaderGroupProps` hook to extend its functionality.
- Custom props may be passed. **NOTE: Custom props will override built-in table props, so be careful!**
### `Column` Properties
The following properties are available on every `Column` object returned by the table instance.
- `id: String`
- The resolved column ID from either the column's `accessor` or the column's hard-coded `id` property
- `visible: Boolean`
- The resolved visible state for the column, derived from the column's `show` property
- `render: Function(type: String | Function | Component, ?props)`
- This function is used to render content in context of a column.
- If `type` is a string, will render using the `column[type]` renderer. React Table ships with default `Header` renderers. Other renderers like `Filter` are available via hooks like `useFilters`.
- If a function or component is passed instead of a string, it will be be passed the table instance and column model as props and is expected to return any valid JSX.
- `getHeaderProps: Function(?props)`
- **Required**
- This function is used to resolve any props needed for this column's header cell.
- You can use the `getHeaderProps` hook to extend its functionality.
- Custom props may be passed. **NOTE: Custom props will override built-in table props, so be careful!**
### `Row` Properties
The following additional properties are available on every `row` object returned by the table instance.
- `cells: Array`
- An array of `Cell` objects containing properties and functions specific to the row and column it belongs to.
- See [Cell Properties](#cell-properties) for more information
- `values: Object`
- A map of this row's **resolved** values by columnID, eg. `{ firstName: 'Tanner', lastName: 'Linsley' }`
- `getRowProps: Function(?props)`
- **Required**
- This function is used to resolve any props needed for this row.
- You can use the `getRowProps` hook to extend its functionality.
- Custom props may be passed. **NOTE: Custom props will override built-in table props, so be careful!**
### `Cell` Properties
The following additional properties are available on every `Cell` object returned in an array of `cells` on every row object.
- `column: Column`
- The corresponding column object for this cell
- `row: Row`
- The corresponding row object for this cell
- `value: any`
- The **resolved** value for this cell.
- By default, this value is displayed on the table via the default `Cell` renderer. To override the way a cell displays
- `getCellProps: Function(?props)`
- **Required**
- This function is used to resolve any props needed for this cell.
- You can use the `getCellProps` hook to extend its functionality.
- Custom props may be passed. **NOTE: Custom props will override built-in table props, so be careful!**
- `render: Function(type: String | Function | Component, ?props)`
- This function is used to render content in context of a cell.
- If `type` is a string, will render using the `column[type]` renderer. React Table ships with a default `Cell` renderer. Other renderers like `Aggregated` are available via hooks like `useFilters`.
- If a function or component is passed instead of a string, it will be be passed the table instance and cell model as props and is expected to return any valid JSX.
### Example
```js
function App() {
const columns = React.useMemo(
() => [
{
Header: 'Name',
columns: [
{
Header: 'First Name',
accessor: 'firstName',
},
{
Header: 'Last Name',
accessor: 'lastName',
},
],
},
],
[]
)
const data = [
{
firstName: 'Tanner',
lastName: 'Linsley',
},
{
firstName: 'Shawn',
lastName: 'Wang',
},
{
firstName: 'Kent C.',
lastName: 'Dodds',
},
{
firstName: 'Ryan',
lastName: 'Florence',
},
]
return
}
function MyTable({ columns, data }) {
const { getTableProps, headerGroups, rows, prepareRow } = useTable({
columns,
data,
})
return (
{headerGroups.map(headerGroup => (
{headerGroup.headers.map(column => (
{column.render('Header')}
))}
))}
{rows.map(
(row, i) =>
prepareRow(row) || (
{row.cells.map(cell => {
return
{cell.render('Cell')}
})}
)
)}
)
}
```
## `useSortBy`
- Plugin Hook
- Optional
`useSortBy` is the hook that implements **row sorting**. It also support multi-sort (keyboard required).
- Multi-sort is enabled by default
- To sort the table via UI, attach the props generated from each column's `getSortByToggleProps()`, then click any of those elements.
- To multi-sort the table via UI, hold `shift` while clicking on any of those same elements that have the props from `getSortByToggleProps()` attached.
- To programmatically sort (or multi-sort) any column, use the `toggleSortBy` method located on the instance or each individual column.
### Table Options
The following options are supported via the main options object passed to `useTable(options)`
- `state[0].sortBy: Array