DataTable Hooks
React hooks for reading the table, debouncing input, building filter options, and keyboard shortcuts.
Hooks you can use inside your table components.
useDataTable
Section titled “useDataTable”Gives any child component the table instance and context.
import { useDataTable } from "@/components/niko-table/core/data-table-context"
export function CustomComponent() { const { table, isLoading, columns } = useDataTable()
const rowCount = table.getFilteredRowModel().rows.length const pageCount = table.getPageCount()
return ( <div> <p>Showing {rowCount} rows</p> <p> Page {table.state.pagination.pageIndex + 1} of {pageCount} </p> </div> )}Return Values
Section titled “Return Values”| Property | Type | Description |
|---|---|---|
table |
DataTableInstance<TData> |
The TanStack Table instance. |
columns |
DataTableColumnDef<TData>[] |
Column definitions array. |
isLoading |
boolean |
Whether the table is in a loading state. |
setIsLoading |
(isLoading: boolean) => void |
Updates the loading state. |
useDebounce
Section titled “useDebounce”Delays a value until the user stops changing it. Good for search inputs and filters.
import { useDebounce } from "@/components/niko-table/hooks/use-debounce"
function SearchFilter() { const [search, setSearch] = useState("") const debouncedSearch = useDebounce(search, 500)
useEffect(() => { // This only runs after user stops typing for 500ms console.log("Searching for:", debouncedSearch) }, [debouncedSearch])
return ( <input value={search} onChange={e => setSearch(e.target.value)} placeholder="Search..." /> )}Parameters
Section titled “Parameters”| Name | Type | Default | Description |
|---|---|---|---|
value |
T |
- | The value to debounce (required). |
delay |
number |
300 |
Delay in milliseconds before the value updates. |
Returns
Section titled “Returns”The debounced value of type T.
useDerivedColumnTitle
Section titled “useDerivedColumnTitle”Works out the title for a column filter.
import { useDerivedColumnTitle } from "@/components/niko-table/hooks/use-derived-column-title"
function ColumnHeader({ column }: { column: Column<Product> }) { // Priority: title prop → column.meta.label → formatted accessorKey const title = useDerivedColumnTitle(column, "first_name") // Returns "FirstName" (formatLabel capitalizes; dashes/underscores become spaces, camelCase does not)
const titleWithMeta = useDerivedColumnTitle(column, "firstName") // Returns column.columnDef.meta.label if set, otherwise "FirstName"
const titleWithOverride = useDerivedColumnTitle(column, "firstName", "Custom Title") // Returns "Custom Title" (explicit title always wins)
return <span>{title}</span>}Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
column |
DataTableColumn<TData> |
The table column. |
accessorKey |
string |
The accessor key of the column. |
title |
string |
Optional title override. |
Returns
Section titled “Returns”The derived title string (priority: title prop → column.meta.label → formatted accessorKey).
useGeneratedOptions
Section titled “useGeneratedOptions”Builds options for select and multiSelect columns from your table data.
import { useGeneratedOptions } from "@/components/niko-table/hooks/use-generated-options"
function FilterPanel({ table }: { table: Table<Product> }) { const optionsByColumn = useGeneratedOptions(table, { showCounts: true, dynamicCounts: true, limitToFilteredRows: true, includeColumns: ["category", "brand"], })
// optionsByColumn.category → [{ value: "electronics", label: "Electronics", count: 42 }, ...] // optionsByColumn.brand → [{ value: "apple", label: "Apple", count: 18 }, ...]
return ( <div> {Object.entries(optionsByColumn).map(([columnId, options]) => ( <FacetedFilter key={columnId} columnId={columnId} options={options} /> ))} </div> )}Parameters
Section titled “Parameters”| Name | Type | Default | Description |
|---|---|---|---|
table |
DataTableInstance<TData> |
- | TanStack Table instance (required). |
config |
GenerateOptionsConfig |
- | Configuration object. |
config.showCounts |
boolean |
true |
Include counts for each option. |
config.dynamicCounts |
boolean |
true |
Recompute counts based on filtered rows. |
config.limitToFilteredRows |
boolean |
true |
Generate options from filtered rows only. |
config.includeColumns |
string[] |
- | Only generate options for these column ids. |
config.excludeColumns |
string[] |
- | Exclude these column ids from generation. |
config.limitPerColumn |
number |
- | Limit number of generated options per column. |
Returns
Section titled “Returns”A record mapping column IDs to arrays of Option objects.
useKeyboardShortcut
Section titled “useKeyboardShortcut”Adds one keyboard shortcut, with control over modifier keys.
import { useKeyboardShortcut } from "@/components/niko-table/hooks/use-keyboard-shortcut"
useKeyboardShortcut({ key: "f", onTrigger: () => setFilterOpen(true), condition: () => !isInputFocused,})
useKeyboardShortcut({ key: "f", requireShift: true, onTrigger: () => clearAllFilters(),})Parameters
Section titled “Parameters”| Name | Type | Default | Description |
|---|---|---|---|
key |
string |
- | The key to listen for (required). |
onTrigger |
() => void |
- | Runs when the shortcut fires. |
enabled |
boolean |
true |
Whether the shortcut is enabled. |
requireShift |
boolean |
false |
Whether to require Shift key. |
requireCtrl |
boolean |
false |
Whether to require Ctrl/Cmd key. |
requireAlt |
boolean |
false |
Whether to require Alt key. |
preventDefault |
boolean |
true |
Block the browser’s default action. |
stopPropagation |
boolean |
false |
Stop the event from bubbling. |
condition |
() => boolean |
- | Return false to skip the shortcut. |
useKeyboardShortcuts
Section titled “useKeyboardShortcuts”Adds many shortcuts at once with one event listener. Same options as useKeyboardShortcut.
import { useKeyboardShortcuts } from "@/components/niko-table/hooks/use-keyboard-shortcut"
useKeyboardShortcuts([ { key: "f", onTrigger: () => setFilterOpen(true) }, { key: "s", onTrigger: () => setSortOpen((prev) => !prev) }, { key: "f", requireShift: true, onTrigger: () => clearFilters() },])Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
shortcuts |
UseKeyboardShortcutOptions[] |
Array of shortcut options (same as useKeyboardShortcut). |
useGeneratedOptionsForColumn
Section titled “useGeneratedOptionsForColumn”A shortcut around useGeneratedOptions for one column.
import { useGeneratedOptionsForColumn } from "@/components/niko-table/hooks/use-generated-options"
const categoryOptions = useGeneratedOptionsForColumn(table, "category", { showCounts: true, dynamicCounts: true,})Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
table |
DataTableInstance<TData> |
TanStack Table instance (required). |
columnId |
string |
The column ID to generate options for. |
config |
GenerateOptionsConfig |
Optional configuration object. |
Returns
Section titled “Returns”An array of Option objects for the specified column.
Changelog
Section titled “Changelog”useGeneratedOptions
Section titled “useGeneratedOptions”- Fix: column definitions now update the options.
- Before: the hook only watched the
tablereference, which stays the same across renders. It returned stale options when column definitions changed (for example, server-side facets updatingmeta.options). - Now:
table.options.columnsis a memo dependency, so the hook recomputes when column definitions change.
- Before: the hook only watched the