useDataTable
The headless hook powering DataTable โ owns data, columns, filters, selection and row identity.
- Overview
- Props
useDataTable is the headless hook that owns all table state. Feed it data + columns (plus
optional filters, selection and row navigation) and it returns a DataTableInstance to hand to
DataTable โ or to any renderer you build yourself.
Optionsโ
| Option | Type | Default | Description |
|---|---|---|---|
data | T[] | โ | The rows to render. |
columns | DataTableColumn<T>[] | โ | Column definitions (see Columns). |
filters | DataTableFilterOptions | โ | Filtering config: { defs, values?, onChange? } (see Filters). |
rowNavigation | DataTableRowNavigation<T> | โ | When set, body rows become whole-row clickable (see Row navigation). |
getRowId | (row: T) => string | โ | Stable row id so row state (e.g. selection) tracks the record, not its array position, across data changes. |
selection | DataTableSelectionOptions<T> | โ | Row-selection config (see Selection). |
pagination | DataTablePaginationOptions | โ | Server-controlled pagination, known total or cursor-based (see Pagination). |
Returnsโ
A DataTableInstance<T> โ pass it whole to <DataTable table={โฆ} /> (or your own renderer). Its slots:
| Slot | Type | Description |
|---|---|---|
table | cherry-picked TanStack table | Rows, headers and selection state (getRowModel(), getHeaderGroups(), โฆ). |
filters | Filters | { defs, values, setValue(key, value), reset() } โ programmatic filter control. |
selection | DataTableSelection<T> | { selectedRows, isAllSelected, isSomeSelected, clear() } โ read/manipulate selection. |
rowNavigation | DataTableRowNavigation<T>? | The row-navigation config, echoed from options for renderers. |
pagination | DataTablePagination? | { pageIndex, pageCount, pageSize?, totalRowCount?, hasUnknownPageCount, setPageIndex(pageIndex), reset() } โ present only when configured. |
5 employees
| Name | Entity | Comment | Status |
|---|---|---|---|
| Foo | Bar | Blabla | Covered |
| Qux | Bar | Onboarding | Not covered |
| Zap | Baz | New hire | Covered |
| Wob | Baz | Left | Not covered |
| Nix | Bar | Paperwork | Processing |
Columnsโ
DataTableColumn<T> is a union discriminated by accessorKey, so each column's cell value type
follows the field it accesses:
accessorKeyโ key into the row object (dotted paths like"values.foo"work).headerโ a string or a render function.cellโ optional cell renderer;info.getValue()is typed from the column'saccessorKey. Return any React node.
The status column in the demo above renders a <Tag> from its cell renderer.
Filtersโ
Filtering is configured with filters: { defs, values?, onChange? } โ defs built with the filter
factories, optional controlled values, and onChange. It is state-only: the hook holds the
filter values and calls onChange(values) on every change โ it does not touch the row model.
You apply the values to data, either client-side or by re-querying your backend.
dataTable.filters also exposes setValue(key, value) and reset() for programmatic control.
5 employees
| Name | Entity | Comment | Status |
|---|---|---|---|
| Foo | Bar | Blabla | Covered |
| Qux | Bar | Onboarding | Not covered |
| Zap | Baz | New hire | Covered |
| Wob | Baz | Left | Not covered |
| Nix | Bar | Paperwork | Processing |
Four factories build the filter defs:
| Factory | Value type | Default | Renders |
|---|---|---|---|
checkboxFilter | string[] | [] | Checkbox group (multi-select). |
singleSelectFilter | string | "" | "" | Radio group. |
textFilter | string | "" | Text input. |
custom | your type | yours | your renderer. |
Trigger labelsโ
Give checkboxFilter and singleSelectFilter a plain string label and the trigger shows the
selection on its own:
- Nothing selected: the name (
Status). - 1-2 selected: the option labels (
Covered, Not covered). - 3+ selected: truncated (
Covered, 2 others).
Pass label as a function of the value to opt out and own the trigger text. The filter's
accessible name uses the empty-value form either way.
Result countsโ
Options of checkboxFilter and singleSelectFilter take an optional count โ how many results the
option matches โ rendered after the label (Covered (2)). The hook holds filter state only, so the
counts are yours to compute: a facet count from your backend, or derived from the loaded rows.
count: 0 renders as (0) instead of disappearing, so an option matching nothing still reads as
empty rather than uncounted.
In the demo below each option counts the rows the other filters keep, so the counts follow the current selection, and the defs are rebuilt on every render. The counts appear only on the options, never in the trigger.
5 employees
| Name | Entity | Comment | Status |
|---|---|---|---|
| Foo | Bar | Blabla | Covered |
| Qux | Bar | Onboarding | Not covered |
| Zap | Baz | New hire | Covered |
| Wob | Baz | Left | Not covered |
| Nix | Bar | Paperwork | Processing |
Selectionโ
Enable with selection: { enabled: true }. The hook tracks the selection state and, when rendered by
DataTable, adds a leading checkbox column (plus a select-all header checkbox in multi-select mode).
Read/act on the selection through dataTable.selection (selectedRows, isAllSelected,
isSomeSelected, clear()).
0 selected
| Name | Entity | Comment | Status | |
|---|---|---|---|---|
| Foo | Bar | Blabla | Covered | |
| Qux | Bar | Onboarding | Not covered | |
| Zap | Baz | New hire | Covered | |
| Wob | Baz | Left | Not covered | |
| Nix | Bar | Paperwork | Processing |
Single selectionโ
selectionType: "single" allows one selected row at a time โ selecting a row replaces the previous
one, and there is no select-all header.
None selected
| Name | Entity | Comment | Status | |
|---|---|---|---|---|
| Foo | Bar | Blabla | Covered | |
| Qux | Bar | Onboarding | Not covered | |
| Zap | Baz | New hire | Covered | |
| Wob | Baz | Left | Not covered | |
| Nix | Bar | Paperwork | Processing |
Disabling rowsโ
canSelectRow returns false to make a row unselectable; its checkbox is disabled.
0 selected
| Name | Entity | Comment | Status | |
|---|---|---|---|---|
| Foo | Bar | Blabla | Covered | |
| Qux | Bar | Onboarding | Not covered | |
| Zap | Baz | New hire | Covered | |
| Wob | Baz | Left | Not covered | |
| Nix | Bar | Paperwork | Processing |
Controlled selectionโ
Omit value for uncontrolled selection (internal state). Pass value (the selected rows) to control
it yourself โ seed it from a persisted field and keep it in sync from onToggle (per row) and
onBulkToggle (select-all, batched). Controlled selection requires getRowId.
1 acknowledged
| Name | Entity | Comment | Status | |
|---|---|---|---|---|
| Foo | Bar | Blabla | Covered | |
| Qux | Bar | Onboarding | Not covered | |
| Zap | Baz | New hire | Covered | |
| Wob | Baz | Left | Not covered | |
| Nix | Bar | Paperwork | Processing |
Paginationโ
Pagination is server-controlled only: the table never slices data. The hook owns the current
page index (0-based, starts at 0); you feed one pageSize-sized page of rows via data and refetch
on onChange. When configured, DataTable renders a pager footer under the table. Two schemes,
discriminated by hasUnknownPageCount:
Known page countโ
For offset APIs reporting the total number of rows: pass totalRowCount (the hook derives the
page count from it and pageSize) and refetch on onChange(pageIndex) (typically by mirroring
the index into your query key).
Cursor-based (unknown page count)โ
For cursor APIs that only report "there is a next page": set hasUnknownPageCount: true and
pass cursor (fetching the page after the current one) and hasNextPage from the latest response.
The hook remembers each visited page's cursor, so the pager can navigate back to any visited page,
and sizes itself from the visited pages plus the promised next one (trailing โฆ while the total
stays unknown). onChange({ cursor, pageIndex }) hands you the cursor fetching the target page
(undefined for the first, uncursored page) โ refetch it, or serve revisits from your own cache
by pageIndex; TanStack's useInfiniteQuery keeps every fetched page (see
TanStack Query).
| Name | Entity | Comment | Status |
|---|---|---|---|
| Foo | Bar | Blabla | Covered |
| Qux | Bar | Onboarding | Not covered |
Move the page programmatically with dataTable.pagination.setPageIndex(pageIndex). A few things
stay your responsibility:
- Reset the page on query changes. When filters, search or any other query input changes, call
dataTable.pagination.reset()alongside โ it returns to page 0 and forgets the visited-pages history (whose cursors belong to the dead query), without firingonChange. The hook won't do it for you: it can't know what feeds your query. - Step back off an empty page. After a deletion empties the current page, move to
Math.min(pageIndex, pageCount - 1)when the refetch lands. - Cross-page selection needs controlled selection.
selection.selectedRowsonly surfaces rows present in the currentdata; to keep selections across pages, passselection.value(+getRowId) and own the full selected list yourself (see Controlled selection).
Row navigationโ
Pass rowNavigation to make whole rows clickable โ onNavigate(row) fires on row click, and
getRowNavigationLabel sets each row's accessible label. The table stays decoupled from routing: you
own the navigation target. The config rides on the returned instance (dataTable.rowNavigation), so
renderers need nothing beyond the instance.
Last viewed: none
| Name | Entity | Comment | Status | |
|---|---|---|---|---|
| Foo | Bar | Blabla | Covered | |
| Qux | Bar | Onboarding | Not covered | |
| Zap | Baz | New hire | Covered | |
| Wob | Baz | Left | Not covered | |
| Nix | Bar | Paperwork | Processing |
Selection and row navigation compose: toggling a checkbox never triggers navigation (the checkbox stops click propagation), while clicking the row content does.
0 selected ยท Last viewed: none
| Name | Entity | Comment | Status | ||
|---|---|---|---|---|---|
| Foo | Bar | Blabla | Covered | ||
| Qux | Bar | Onboarding | Not covered | ||
| Zap | Baz | New hire | Covered | ||
| Wob | Baz | Left | Not covered | ||
| Nix | Bar | Paperwork | Processing |
Coming soonโ
Not available yet:
- client-side sorting,
- client-side pagination (server-controlled pagination is available, see Pagination),
- expandable / nested rows.