Skip to content
Niko Table

DataTable Hooks

React hooks for reading the table, debouncing input, building filter options, and keyboard shortcuts.

Hooks you can use inside your table components.

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

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..."
/>
)
}
Name Type Default Description
value T - The value to debounce (required).
delay number 300 Delay in milliseconds before the value updates.

The debounced value of type T.

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>
}
Name Type Description
column DataTableColumn<TData> The table column.
accessorKey string The accessor key of the column.
title string Optional title override.

The derived title string (priority: title prop → column.meta.label → formatted accessorKey).

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

A record mapping column IDs to arrays of Option objects.

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(),
})
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.

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() },
])
Name Type Description
shortcuts UseKeyboardShortcutOptions[] Array of shortcut options (same as useKeyboardShortcut).

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,
})
Name Type Description
table DataTableInstance<TData> TanStack Table instance (required).
columnId string The column ID to generate options for.
config GenerateOptionsConfig Optional configuration object.

An array of Option objects for the specified column.


  • Fix: column definitions now update the options.
    • Before: the hook only watched the table reference, which stays the same across renders. It returned stale options when column definitions changed (for example, server-side facets updating meta.options).
    • Now: table.options.columns is a memo dependency, so the hook recomputes when column definitions change.