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.
| Name | Entity | Status |
|---|---|---|
| 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
| Name | Entity | Status |
|---|---|---|
| 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
| Name | Entity | Status | |
|---|---|---|---|
| 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):
datais the current page's rows, looked up indata.pagesby your page-index state;cursor/hasNextPagedescribe the current page (itsnextCursor);onChangeonly fetches when the target page is past the frontier (fetchNextPage()) — visited pages come straight from the infinite query's cache.
Page 1
| Name | Entity | Status |
|---|---|---|
| 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.