React Table Component Examples
Copy-paste React Table examples: a basic data table, striped and bordered variants, a pricing table with footer, status badges in cells, and column sorting.
Most React tables start the same way: records from an API, a header row, and a request to keep it readable on a phone. Drivn's Table answers with plain semantic markup and nothing more. One Table export carries seven parts through dot notation — Table.Header, Table.Body, Table.Footer, Table.Row, Table.Head, Table.Cell, and Table.Caption — and every class those parts use sits in a single const styles object at the top of the file. The only import beyond React is cn() from @/utils/cn, so the component adds no runtime UI dependency to your bundle.
Two defaults matter before you write a single row. The file has no 'use client' directive, so a table that only displays data renders as a server component and ships no JavaScript for the grid. And Table.Head sets scope="col" unless you override it, which gives screen readers the column association without extra attributes. The root also wraps the <table> in a w-full overflow-x-auto div, so wide grids scroll sideways on small screens instead of stretching the page.
The examples below are TypeScript files that import from @/components/ui/table, the path the Drivn CLI writes to. They cover a basic grid, the striped and bordered variants, a pricing table with a caption and footer, a status column using Badge, and a sortable column with a keyboard-reachable header button.
A basic data table
The starting point is a Table with a Table.Header row of Table.Head cells and a Table.Body of Table.Row / Table.Cell pairs. The header row gets a bottom border from the styles.header entry ([&_tr]:border-b), each body row carries border-b border-border plus a hover:bg-muted/40 highlight, and the last body row drops its border via [&_tr:last-child]:border-0. The whole table sits inside a w-full overflow-x-auto wrapper so narrow viewports scroll horizontally instead of breaking the layout.
Because nothing here is interactive, this is a pure server component — no 'use client', no hooks, no client bundle cost for the table. Drop it straight into a page or a server-rendered Card. The dot notation keeps the import to a single import { Table } line, and the component tree reads top to bottom like the <thead> / <tbody> structure it produces.
1 import { Table } from "@/components/ui/table" 2 3 export default function Page() { 4 return ( 5 <Table> 6 <Table.Header> 7 <Table.Row> 8 <Table.Head>Name</Table.Head> 9 <Table.Head>Role</Table.Head> 10 <Table.Head>Status</Table.Head> 11 </Table.Row> 12 </Table.Header> 13 <Table.Body> 14 <Table.Row> 15 <Table.Cell>Alice</Table.Cell> 16 <Table.Cell>Engineer</Table.Cell> 17 <Table.Cell>Active</Table.Cell> 18 </Table.Row> 19 <Table.Row> 20 <Table.Cell>Bob</Table.Cell> 21 <Table.Cell>Designer</Table.Cell> 22 <Table.Cell>Active</Table.Cell> 23 </Table.Row> 24 </Table.Body> 25 </Table> 26 ) 27 }
Striped and bordered variants
For long lists, zebra striping makes rows easier to track across columns. Pass variant="striped" on the Table root and the styles.variants.striped entry applies [&_tbody_tr:nth-child(even)]:bg-muted/30, tinting every even body row. For dense data grids where you want a cell on every boundary, variant="bordered" adds [&_th]:border [&_td]:border with border-border plus the same even-row striping. The variant prop is typed as keyof typeof styles.variants, so your editor autocompletes default, striped, and bordered and rejects anything else.
Neither variant requires extra classes on the rows or cells — the arbitrary Tailwind selectors live in the root's styles.variants object and cascade down. Swap a plain table to striped by changing one prop, no markup churn. This is the example to reach for on a transactions list, a user directory, or any table long enough that row tracking becomes a usability problem.
1 import { Table } from "@/components/ui/table" 2 3 export default function StripedTable() { 4 return ( 5 <Table variant="striped"> 6 <Table.Header> 7 <Table.Row> 8 <Table.Head>Name</Table.Head> 9 <Table.Head>Role</Table.Head> 10 <Table.Head>Status</Table.Head> 11 </Table.Row> 12 </Table.Header> 13 <Table.Body> 14 <Table.Row> 15 <Table.Cell>Alice</Table.Cell> 16 <Table.Cell>Engineer</Table.Cell> 17 <Table.Cell>Active</Table.Cell> 18 </Table.Row> 19 <Table.Row> 20 <Table.Cell>Bob</Table.Cell> 21 <Table.Cell>Designer</Table.Cell> 22 <Table.Cell>Active</Table.Cell> 23 </Table.Row> 24 <Table.Row> 25 <Table.Cell>Charlie</Table.Cell> 26 <Table.Cell>PM</Table.Cell> 27 <Table.Cell>Away</Table.Cell> 28 </Table.Row> 29 </Table.Body> 30 </Table> 31 ) 32 }
A status column built with Badge
Raw status text like "Active" or "Failed" is hard to scan in a busy table. Render a Badge inside the status Table.Cell instead, and the color does the scanning for you. The Badge variant prop maps cleanly onto states — success for active, outline for idle or away, destructive for failed or blocked. Keep the row data in an array and map it to Table.Row elements so the table stays declarative as the dataset changes.
This pattern is the backbone of any operational dashboard — deploys, jobs, users, invoices — where a glance should tell you which rows need attention. The Table primitives stay server-renderable; the Badge is equally static, so the whole table is still a server component with no client JavaScript. Add a Pagination control underneath once the list outgrows one screen.
1 import { Table } from "@/components/ui/table" 2 import { Badge } from "@/components/ui/badge" 3 4 const rows = [ 5 { name: "Alice", role: "Engineer", status: "Active" }, 6 { name: "Bob", role: "Designer", status: "Away" }, 7 { name: "Charlie", role: "PM", status: "Failed" }, 8 ] 9 10 const variants = { 11 Active: "success", 12 Away: "outline", 13 Failed: "destructive", 14 } as const 15 16 export default function StatusTable() { 17 return ( 18 <Table> 19 <Table.Header> 20 <Table.Row> 21 <Table.Head>Name</Table.Head> 22 <Table.Head>Role</Table.Head> 23 <Table.Head>Status</Table.Head> 24 </Table.Row> 25 </Table.Header> 26 <Table.Body> 27 {rows.map((row) => ( 28 <Table.Row key={row.name}> 29 <Table.Cell>{row.name}</Table.Cell> 30 <Table.Cell>{row.role}</Table.Cell> 31 <Table.Cell> 32 <Badge variant={variants[row.status]}> 33 {row.status} 34 </Badge> 35 </Table.Cell> 36 </Table.Row> 37 ))} 38 </Table.Body> 39 </Table> 40 ) 41 }
Install Drivn in one command
Copy the source into your project and own every line. Zero runtime dependencies, pure React + Tailwind.
npx drivn@latest createRequires Node 18+. Works with npm, pnpm, and yarn.
Frequently asked questions
Pass variant="striped" on the Table root. The styles.variants.striped entry applies [&_tbody_tr:nth-child(even)]:bg-muted/30, which tints every even row in the body. There is no per-row className needed — the arbitrary Tailwind selector lives on the root table and cascades down. For bordered cells plus striping, use variant="bordered" instead, which adds border utilities to every th and td as well.
Static tables are server components. The Table source has no 'use client' directive and no hooks, so it renders as pure semantic markup with zero client JavaScript. You only opt into a client component when you add interactivity like column sorting — and even then only the wrapper that owns the useState needs 'use client', not the Table.Header, Table.Row, or Table.Cell primitives themselves.
Pass align="right" on both the Table.Head and the Table.Cell for that column. Drivn forwards align as the native HTML attribute, and the head and cell style entries include [&[align=right]]:text-right, a Tailwind attribute selector that right-aligns the text. This keeps numbers visually aligned without adding className="text-right" to every cell in the column.
Yes. Render a Badge component inside the status Table.Cell and map the row status to a Badge variant — success for active, outline for idle, destructive for failed. Both Table and Badge are dependency-free and server-renderable, so the whole table stays a server component. Keep the row data in an array and map it to Table.Row elements so the markup stays declarative.
Hold the sort direction in React.useState, derive a sorted copy with [...data].sort(), and toggle the direction from a native button rendered inside Table.Head, with aria-sort set on the head. The Table has no internal state, so you own the comparator and render the sorted order. Only the wrapper needs 'use client'. For per-column sorting plus row selection and pagination, use the Drivn Data Table, which builds those features on the same Table primitives.
Yes. The Table root wraps its table element in a div with w-full overflow-x-auto, so on narrow viewports the table scrolls horizontally instead of overflowing or wrapping its columns. You do not need to add a scroll container yourself — it is built into the wrapper. For very wide tables, this keeps the page layout intact while letting users swipe across the columns.