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
  • Examples

Examples

Loading data with TanStack Query​

All demos below run against a fake API with ~600ms latency, so loading states are visible.

Load with useQuery​

A useQuery whose result is the table's data (default it to []), with isPending passed to loading — skeletons on first load only.

Employees
NameEntityStatus
Loading…‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌

The Refresh button reloads with queryClient.resetQueries(...), which drops the cached data so the query returns to pending and the loading message replays. To keep showing the current rows while reloading in the background instead, use refetch() — or, from anywhere else (e.g. after a mutation), queryClient.invalidateQueries({ queryKey: ["employees"] }).

Reloading when a filter changes​

Filters on useDataTable are state-only: the hook holds the values and tells you when they change — applying them is your job, here by re-querying the backend. Use controlled filters: keep the values in your own state, pass them as filters.values, and update them from filters.onChange. That same state goes into the query key, so any filter change refetches automatically — no manual refetch() call.

placeholderData: keepPreviousData keeps the previous rows on screen while the new ones load, instead of flashing an empty table on every keystroke. Combined with loading={isFetching}, loading picks the right visual by itself: skeleton on the very first load, overlay over the kept rows on every refetch. The paginated demos below follow the same pattern.

0 employees

Employees
NameEntityStatus
Loading…‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌

Pagination with a known page count​

For offset APIs that return the total (totalRowCount here): keep the page index in state, key the query on it, and hand the index changes back through pagination.onChange. Each page becomes its own cache entry, so previously visited pages render instantly from cache while revalidating.

The demo combines pagination with filters: both feed the query key, and the filter onChange resets the page with dataTable.pagination.reset() — the old index is meaningless against the new result set, and the hook can't know what else feeds your query.

The demo also enables selection across pages. Since the hook only ever sees the current page's rows, use controlled selection: own the full selected list in your state, pass it as selection.value (with getRowId), and update it from onToggle / onBulkToggle. Selections then survive paging, and the header select-all only applies to the visible page.

Page 1 of 0

0 selected

Employees
NameEntityStatus
Loading…‌
‌
‌
‌
‌
‌
‌
‌
‌

Pagination with useInfiniteQuery​

For cursor APIs, useInfiniteQuery is the natural fit: it accumulates every fetched page (each with its nextCursor) and getNextPageParam chains the cursors for you. Wire it to the hook's cursor scheme (hasUnknownPageCount: true):

  • data is the current page's rows, looked up in data.pages by your page-index state;
  • cursor / hasNextPage describe the current page (its nextCursor);
  • onChange only fetches when the target page is past the frontier (fetchNextPage()) — visited pages come straight from the infinite query's cache.

Page 1

Employees
NameEntityStatus
Loading…‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌

The pager shows every visited page plus the promised next one, with a trailing … while the total stays unknown. Visited pages stay reachable — served instantly from the infinite query's cache.

useInfiniteQuery has no keepPreviousData option, so the demo hand-rolls it: pages[pageIndex] ?? pages.at(-1) keeps the last fetched page on screen while the next one loads.

Edit this page
Previous
DataTable
  • Loading data with TanStack Query
    • Load with useQuery
  • Reloading when a filter changes
  • Pagination with a known page count
  • Pagination with useInfiniteQuery