Data Table
A powerful and flexible data table component for displaying, filtering, sorting, and paginating tabular data.
Reference the TanStack Table Column Definitions Guide for detailed column definition guide.
Features
- Advanced Filtering - Multiple filter types (text, number, date, select, multi-select) with customizable operators
- URL State Management - Sync table state with URL search params using nuqs
- Sorting - Multi-column sorting with persistent state
- Pagination - Server-side or client-side pagination with customizable page sizes
- Column Visibility - Toggle column visibility with view options menu
- Row Selection - Single or multi-row selection with action bar
- Column Pinning - Pin columns to left or right for better UX
- Keyboard Navigation - Full keyboard support with shortcuts for filter and sort menus
- Responsive - Mobile-first design with overflow handling
- Customizable - Flexible API for custom filters, columns, and actions
Layout
Import all components and combine them:
Walkthrough
Define columns with appropriate metadata:
Initialize the table state using the useDataTable hook:
Pass the table instance to the DataTable, and DataTableToolbar components:
Render an action bar on row selection:
API Reference
Column Definitions
The column definitions are used to define the columns of the data table.
Properties
Core configuration options for defining columns.
| Prop | Description |
|---|---|
id | Required: Unique identifier for the column |
accessorKey | Required: Key to access the data from the row |
accessorFn | Optional: Custom accessor function to access data |
header | Optional: Custom header component with column props |
cell | Optional: Custom cell component with row props |
meta | Optional: Meta options for accessing column metadata |
enableColumnFilter | By default, the column will not be filtered. Set to `true` to enable filtering |
enableSorting | Enable sorting for this column |
enableHiding | Enable column visibility toggle |
Column Meta
Column meta options for filtering, sorting, and view options.
| Prop | Description |
|---|---|
label | The display name for the column |
placeholder | The placeholder text for filter inputs |
variant | The type of filter to use (`text`, `number`, `select`, etc.) |
options | For select/multi-select filters, an array of options with `label`, `value`, and optional `count` and `icon` |
range | For range filters, a tuple of `[min, max]` values |
unit | For numeric filters, the unit to display (e.g., 'hr', '$') |
icon | The react component to use as an icon for the column |
Filter Variants
Available filter variants for column meta.
| Title | Description |
|---|---|
text | Text search with contains, equals, etc. |
number | Numeric filters with equals, greater than, less than, etc. |
range | Range filters with minimum and maximum values |
date | Date filters with equals, before, after, etc. |
dateRange | Date range filters with start and end dates |
boolean | Boolean filters with true/false values |
select | Single-select filters with predefined options |
multiSelect | Multi-select filters with predefined options |
useDataTable
A hook for initializing the data table with state management.
Overview
The useDataTable hook acts as an intelligent wrapper around useReactTable from @tanstack/react-table. It seamlessly handles:
- Server-side pagination, sorting, and filtering
- Controlled or uncontrolled state for page and page size
- Debounced column filter updates (defaults to 300ms) to prevent excessive API calls
- Row selection and column visibility management
Import
Usage
Basic Setup
First, define your columns. Use the meta property to configure filter behaviors.
Controlled Pagination (Recommended for API integrations)
When fetching data from an external API, it's highly recommended to control the pagination state externally.
Uncontrolled Pagination
If you don't provide page, perPage, or onPageChange, the hook will manage the state internally.
Column Filtering
Column filtering is enabled by setting enableColumnFilter: true in your column definitions. The filterValues returned by the hook contains the active filter key-value pairs that can be sent directly to your API:
Props
The useDataTable hook accepts the following properties.
Prop
Type
Returns
The useDataTable hook returns an object with the following properties.
Prop
Type
Column Meta for Filter Variant
Utilize the meta property in your column definitions to configure the UI representation of the filter:
Loading State (DataTableSkeleton)
DataTableSkeleton is used to display a loading skeleton that strictly matches the actual layout of the data table. This is extremely useful when combined with React.Suspense or when awaiting data from an API.
Skeleton Usage
Suspense Integration
The recommended pattern when using frameworks like Next.js or React Router v7 with asynchronous data fetching:
Skeleton Props
| Prop | Type | Default | Description |
|---|---|---|---|
columnCount | number | — | Required. The number of columns the skeleton should render. |
rowCount | number | 10 | The number of row skeletons to display. |
filterCount | number | 0 | The number of filter skeleton boxes to display in the top toolbar. |
cellWidths | string[] | ['auto'] | An array of column widths (e.g., ['100px', '200px']). |
withViewOptions | boolean | true | Whether to render the skeleton for the column visibility toggle button. |
withPagination | boolean | true | Whether to render the skeleton for the bottom pagination controls. |
shrinkZero | boolean | false | If true, columns will possess a min-width matching their respective cellWidths. |
className | string | — | Custom CSS class for the main container. |
How is this guide?