Skip to main content
Brand and assets
Build
Monitor and contribute
/
Patterns & layouts
  • Overview
  • Layouts
  • PageLayout
  • FullPageLayout
  • Patterns
  • Forms & density
  • Composition (asChild)
  • Rich components
  • DataTable
    • useDataTable
    • DataTable
    • Examples
  • DataTable
  • useDataTable

useDataTable

The headless hook powering DataTable โ€” owns data, columns, filters, selection and row identity.

Health
Component health
Source
View on Github
Storybook
View in Storybook
Playground
No Example
Accessibility
unknown
Platform
Web
  • 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โ€‹

OptionTypeDefaultDescription
dataT[]โ€”The rows to render.
columnsDataTableColumn<T>[]โ€”Column definitions (see Columns).
filtersDataTableFilterOptionsโ€”Filtering config: { defs, values?, onChange? } (see Filters).
rowNavigationDataTableRowNavigation<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.
selectionDataTableSelectionOptions<T>โ€”Row-selection config (see Selection).
paginationDataTablePaginationOptionsโ€”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:

SlotTypeDescription
tablecherry-picked TanStack tableRows, headers and selection state (getRowModel(), getHeaderGroups(), โ€ฆ).
filtersFilters{ defs, values, setValue(key, value), reset() } โ€” programmatic filter control.
selectionDataTableSelection<T>{ selectedRows, isAllSelected, isSomeSelected, clear() } โ€” read/manipulate selection.
rowNavigationDataTableRowNavigation<T>?The row-navigation config, echoed from options for renderers.
paginationDataTablePagination?{ pageIndex, pageCount, pageSize?, totalRowCount?, hasUnknownPageCount, setPageIndex(pageIndex), reset() } โ€” present only when configured.

5 employees

Employees
NameEntityCommentStatus
FooBarBlabla

Covered

QuxBarOnboarding

Not covered

ZapBazNew hire

Covered

WobBazLeft

Not covered

NixBarPaperwork

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's accessorKey. 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

Employees
NameEntityCommentStatus
FooBarBlabla

Covered

QuxBarOnboarding

Not covered

ZapBazNew hire

Covered

WobBazLeft

Not covered

NixBarPaperwork

Processing

Four factories build the filter defs:

FactoryValue typeDefaultRenders
checkboxFilterstring[][]Checkbox group (multi-select).
singleSelectFilterstring | """"Radio group.
textFilterstring""Text input.
customyour typeyoursyour 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

Employees
NameEntityCommentStatus
FooBarBlabla

Covered

QuxBarOnboarding

Not covered

ZapBazNew hire

Covered

WobBazLeft

Not covered

NixBarPaperwork

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

Employees
NameEntityCommentStatus
FooBarBlabla

Covered

QuxBarOnboarding

Not covered

ZapBazNew hire

Covered

WobBazLeft

Not covered

NixBarPaperwork

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

Employees
NameEntityCommentStatus
FooBarBlabla

Covered

QuxBarOnboarding

Not covered

ZapBazNew hire

Covered

WobBazLeft

Not covered

NixBarPaperwork

Processing

Disabling rowsโ€‹

canSelectRow returns false to make a row unselectable; its checkbox is disabled.

0 selected

Employees
NameEntityCommentStatus
FooBarBlabla

Covered

QuxBarOnboarding

Not covered

ZapBazNew hire

Covered

WobBazLeft

Not covered

NixBarPaperwork

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

Employees
NameEntityCommentStatus
FooBarBlabla

Covered

QuxBarOnboarding

Not covered

ZapBazNew hire

Covered

WobBazLeft

Not covered

NixBarPaperwork

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).

Employees
NameEntityCommentStatus
FooBarBlabla

Covered

QuxBarOnboarding

Not covered

1
2
3

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).

Employees
NameEntityCommentStatus
FooBarBlabla

Covered

QuxBarOnboarding

Not covered

1
2

...

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 firing onChange. 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.selectedRows only surfaces rows present in the current data; to keep selections across pages, pass selection.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

Employees
NameEntityCommentStatus
FooBarBlabla

Covered

QuxBarOnboarding

Not covered

ZapBazNew hire

Covered

WobBazLeft

Not covered

NixBarPaperwork

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

Employees
NameEntityCommentStatus
FooBarBlabla

Covered

QuxBarOnboarding

Not covered

ZapBazNew hire

Covered

WobBazLeft

Not covered

NixBarPaperwork

Processing

Coming soonโ€‹

Not available yet:

  • client-side sorting,
  • client-side pagination (server-controlled pagination is available, see Pagination),
  • expandable / nested rows.

useDataTable props

No props
Edit this page
Previous
Overview
Next
DataTable
  • Options
  • Returns
  • Columns
  • Filters
    • Trigger labels
    • Result counts
  • Selection
    • Single selection
    • Disabling rows
    • Controlled selection
  • Pagination
    • Known page count
    • Cursor-based (unknown page count)
  • Row navigation
  • Coming soon