Infinite Scroll Table
Load more rows as the user scrolls. Good for tables backed by a paginated API.
Loads more rows when the user scrolls to the bottom, by calling your loadMore handler. Use it for cursor-paginated APIs and feeds with a small to medium number of rows. Heads up: every loaded row stays in the DOM. See the caution below for when to use the virtualized variant instead.
Preview with Controlled State
Introduction
Section titled “Introduction”The Infinite Scroll Table loads more rows as the user scrolls toward the bottom. It fits cursor-paginated APIs, activity feeds, and long product lists where loading everything upfront is wasteful.
Niko Table gives you two pieces for this:
onScrolledBottomonDataTableBody: the scroll trigger.DataTableLoadingMore: the “loading more” indicator.
To set it up, add one child to the body and wire one callback.
This example uses a mocked in-memory API with artificial delay. You can see every state: the first skeleton, the next-page spinner rows, and the end-of-results marker. No real backend needed.
Installation
Section titled “Installation”Install the DataTable core and add-ons for this example:
pnpm dlx shadcn@latest add @niko-table/data-table @niko-table/data-table-search-filter @niko-table/data-table-view-menu @niko-table/data-table-column-sortFirst time using
@niko-table? See the Installation Guide to set up the registry.
Prerequisites
Section titled “Prerequisites”We are going to build a table that paginates through a list of products. Here’s what our data looks like:
type Product = { id: string name: string category: string price: number stock: number status: "in-stock" | "low-stock" | "out-of-stock"}Implementation
Section titled “Implementation”Deterministic mock data generator
Section titled “Deterministic mock data generator”One rule for demos: don’t use Math.random() or Date.now() in mock data. If someone copies this file into a Next.js, Remix, or RSC app, those calls give different values on the server and the client. React then logs a hydration mismatch on every row. Use index-based values instead:
const CATEGORIES = [ "Electronics", "Clothing", "Food", "Books", "Sports", "Home", "Toys", "Beauty",] as const
function generateMockProducts(count: number): Product[] { return Array.from({ length: count }, (_, i) => { const stock = (i * 37) % 150 const price = ((i * 13) % 490) + 10 return { id: `product-${i + 1}`, name: `Product ${i + 1}`, category: CATEGORIES[i % CATEGORIES.length], price, stock, status: stock === 0 ? "out-of-stock" : stock < 20 ? "low-stock" : "in-stock", } })}
// The "server" — a 500-item pool we paginate through.const TOTAL_POOL = generateMockProducts(500)const PAGE_SIZE = 20const FAKE_LATENCY_MS = 800
// Simulate a paginated API endpoint.function fetchNextPage(offset: number): Promise<Product[]> { return new Promise(resolve => { setTimeout(() => { resolve(TOTAL_POOL.slice(offset, offset + PAGE_SIZE)) }, FAKE_LATENCY_MS) })}With a real API (TanStack Query’s useInfiniteQuery, SWR’s useSWRInfinite, tRPC, a plain fetch, and so on) this problem goes away, because real API data is already stable.
Pagination state + loadMore handler
Section titled “Pagination state + loadMore handler”The component tracks three pieces of state:
loaded: the rows fetched so far.isLoading: true during the first fetch (drivesDataTableSkeleton).isFetching: true during any later next-page fetch (drivesDataTableLoadingMore).
const [loaded, setLoaded] = React.useState<Product[]>([])const [isLoading, setIsLoading] = React.useState(true)const [isFetching, setIsFetching] = React.useState(false)
// Derive "is there more to fetch?" from the accumulator size.const hasMore = loaded.length < TOTAL_POOL.length
// Kick off the first page on mount.React.useEffect(() => { let cancelled = false void fetchNextPage(0).then(page => { if (cancelled) return setLoaded(page) setIsLoading(false) }) return () => { cancelled = true }}, [])
const loadMore = React.useCallback(async () => { // Double-guard: never fire while a fetch is in flight, never // fire when the pool is exhausted. if (isFetching || !hasMore) return setIsFetching(true) try { const nextPage = await fetchNextPage(loaded.length) setLoaded(prev => [...prev, ...nextPage]) } finally { setIsFetching(false) }}, [isFetching, hasMore, loaded.length])<DataTableBody /> with onScrolledBottom + <DataTableLoadingMore />
Section titled “<DataTableBody /> with onScrolledBottom + <DataTableLoadingMore />”DataTableBody calls onScrolledBottom when the user scrolls within scrollThreshold pixels of the bottom. DataTableLoadingMore is a child that shows itself based on its own isFetching prop. Render it always, and it appears and disappears as fetches start and end.
"use client"
import * as React from "react"import { DataTableRoot } from "@/components/niko-table/core/data-table-root"import { DataTable } from "@/components/niko-table/core/data-table"import { DataTableHeader, DataTableBody, DataTableEmptyBody, DataTableSkeleton, DataTableLoadingMore,} from "@/components/niko-table/core/data-table-structure"import { DataTableSearchFilter } from "@/components/niko-table/components/data-table-search-filter"import { DataTableToolbarSection } from "@/components/niko-table/components/data-table-toolbar-section"import { DataTableViewMenu } from "@/components/niko-table/components/data-table-view-menu"
export function InfiniteScrollTable() { // ... state + loadMore from the previous section ...
return ( <DataTableRoot data={loaded} columns={columns} isLoading={isLoading} config={{ initialPageSize: 500 }} > <DataTableToolbarSection> <DataTableSearchFilter placeholder="Search products..." /> <DataTableViewMenu /> </DataTableToolbarSection>
<DataTable className="max-h-[600px] rounded-lg border"> <DataTableHeader /> <DataTableBody scrollThreshold={200} onScrolledBottom={() => { if (hasMore && !isFetching) void loadMore() }} > <DataTableSkeleton rows={10} /> <DataTableEmptyBody /> <DataTableLoadingMore isFetching={isFetching}> Loading more products... </DataTableLoadingMore> </DataTableBody> </DataTable> </DataTableRoot> )}Required: Fixed Height
Section titled “Required: Fixed Height”Infinite scroll needs a scroll container. Without a fixed max-h-* (or height) on <DataTable>, the table grows with its content and has nothing to scroll. Then onScrolledBottom never fires and loadMore silently does nothing.
<DataTable className="max-h-[600px] rounded-lg border"> {/* ... */}</DataTable>Guard Against Double-Fetches
Section titled “Guard Against Double-Fetches”Always guard loadMore against running twice at once. Scroll events can fire in quick bursts (especially momentum scroll on mobile). onScrolledBottom can then fire several times before React sets isFetching to true.
onScrolledBottom={() => { if (hasMore && !isFetching) void loadMore()}}
const loadMore = React.useCallback(async () => { // Double-guard inside the handler itself. if (isFetching || !hasMore) return // ...}, [isFetching, hasMore])When to Use
Section titled “When to Use”✅ Use this non-virtualized variant when:
- Your dataset is small (under ~200 rows) and virtualization is overkill
- You need variable row heights that don’t work well with
estimateSize - You want the simplest setup, with no
@tanstack/react-virtualdependency
⚠️ For most infinite-scroll tables, use the Infinite Scroll Virtualized Table instead. It renders only visible rows, so performance stays flat even after thousands of rows load.
❌ Don’t use Infinite Scroll Table when:
- You need client-side sorting or filtering across the whole dataset (you can’t sort rows you haven’t fetched). Use server-side sort and filter, or fetch all rows upfront
- Users need to bookmark or link to a specific row (infinite scroll has no stable page URLs)
- The dataset fits in one request (use Basic Table)
Next Steps
Section titled “Next Steps”- Virtualization Table: for large datasets already fully loaded
- Infinite Scroll Virtualized Table: combine both for massive paginated datasets
- Server-Side Table: when you need server-driven sort/filter with traditional pagination