Sub topic 13
Building data tables with TanStack Table
This chapter assumes you know React hooks and TypeScript basics. It also assumes you have read the earlier chapters on headless components and on customizing components you own. You will build a table with TanStack Table, then swap in shadcn/ui markup.
Why a table needs a headless library
A data table needs sorting, filtering, pagination, and row selection. These behaviors are tedious to write by hand, and they have nothing to do with how the table looks. That split is the headless idea from earlier in this tutorial.
TanStack Table is a headless library. It keeps table state and computes rows. It renders no markup. You write the <table> elements and apply your own classes.
The working model
You give the library two things: data (an array of row objects) and columns (a list of column definitions). You also pass row models, which are functions that turn raw data into the rows to display. The hook useReactTable returns a table instance. You read headers and rows from it and render them.
Use the diagram below for the data flow.
A minimal table
import {
useReactTable,
getCoreRowModel,
flexRender,
type ColumnDef,
} from "@tanstack/react-table"
type User = { name: string; email: string }
const columns: ColumnDef<User>[] = [
{ accessorKey: "name", header: "Name" },
{ accessorKey: "email", header: "Email" },
]
export function UserTable({ data }: { data: User[] }) {
const table = useReactTable({ data, columns, getCoreRowModel: getCoreRowModel() })
return (
<table>
<thead>
{table.getHeaderGroups().map((group) => (
<tr key={group.id}>
{group.headers.map((header) => (
<th key={header.id}>
{flexRender(header.column.columnDef.header, header.getContext())}
</th>
))}
</tr>
))}
</thead>
<tbody>
{table.getRowModel().rows.map((row) => (
<tr key={row.id}>
{row.getVisibleCells().map((cell) => (
<td key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</td>
))}
</tr>
))}
</tbody>
</table>
)
}Key parts:
accessorKeytells the library which property of each row to read.getCoreRowModelis the required base row model. Sorting, filtering, and pagination each add another row model that you opt into.flexRenderrenders a column'sheaderorcelldefinition. That definition can be a string or a React component.row.idandheader.idare used as keys. Do not use array indexes.
Using shadcn/ui table components
Add the styled table parts with the CLI:
npx shadcn@latest add tableThis copies a table.tsx file into your project, as with other components. It exports Table, TableHeader, TableBody, TableRow, TableHead, and TableCell. They wrap the plain HTML elements and add Tailwind classes. Replace <table> with <Table>, <thead> with <TableHeader>, and so on. The logic stays the same, because only the markup changed.
Common errors
- Columns or data recreated on every render. Defining
columnsor a freshdataarray inside the component makes the table recompute constantly and can cause render loops. Definecolumnsoutside the component, or wrap them inuseMemo. Keepdatastable, for example in state. - Missing row model. Setting up sorting without
getSortedRowModelhas no visible effect. Each feature needs its row model. - Skipping
flexRender. Printingheader.column.columnDef.headerdirectly breaks when the definition is a function. - Lost semantics. Keep real table elements. Replacing them with
divelements removes the roles that screen readers use.
Exercise
A teammate adds a sort toggle to a header but clicking it does not reorder the rows. The table uses only getCoreRowModel. What is missing, and why?
Show answer
The table needs getSortedRowModel: getSortedRowModel() passed to useReactTable, along with sorting state and a way to toggle it. TanStack Table is headless and opt-in. The core row model returns rows in their original order, so a sorted row model must be added for sorting to take effect.