Skip to content
Drivn
6 min read

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.

1import { Table } from "@/components/ui/table"
2
3export 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.

1import { Table } from "@/components/ui/table"
2
3export 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.

1import { Table } from "@/components/ui/table"
2import { Badge } from "@/components/ui/badge"
3
4const rows = [
5 { name: "Alice", role: "Engineer", status: "Active" },
6 { name: "Bob", role: "Designer", status: "Away" },
7 { name: "Charlie", role: "PM", status: "Failed" },
8]
9
10const variants = {
11 Active: "success",
12 Away: "outline",
13 Failed: "destructive",
14} as const
15
16export 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}

A sortable column with an accessible header button

Sorting is the first point where a table needs state, so this is the one example marked 'use client'. Hold the direction in React.useState, derive a sorted copy with [...data].sort() so the source array stays untouched, and render whatever order comes out. The Drivn Table keeps no state of its own, so the comparator, the toggle, and the initial order all live in your wrapper while the primitives just paint rows.

Put the click target on a native <button type="button"> inside Table.Head rather than on the <th> itself. A button is reachable with Tab and fires on Enter and Space; a clickable header cell is invisible to keyboard users. Set aria-sort on the head, which Table.Head spreads onto the <th>, so assistive tech announces the current direction. When you need sorting on any column plus row selection, a loading state, and page controls together, the Data Table builds all of that on these same Table primitives from a columns definition and a sortable prop.

1"use client"
2
3import * as React from "react"
4import { Table } from "@/components/ui/table"
5
6interface Row {
7 name: string
8 score: number
9}
10
11const data: Row[] = [
12 { name: "Alice", score: 92 },
13 { name: "Bob", score: 78 },
14 { name: "Charlie", score: 85 },
15]
16
17export default function SortableTable() {
18 const [asc, setAsc] = React.useState(true)
19
20 const sorted = [...data].sort((a, b) =>
21 asc ? a.score - b.score : b.score - a.score
22 )
23
24 return (
25 <Table>
26 <Table.Header>
27 <Table.Row>
28 <Table.Head>Name</Table.Head>
29 <Table.Head
30 align="right"
31 aria-sort={asc ? "ascending" : "descending"}
32 >
33 <button
34 type="button"
35 onClick={() => setAsc((v) => !v)}
36 className="cursor-pointer select-none"
37 >
38 Score {asc ? "↑" : "↓"}
39 </button>
40 </Table.Head>
41 </Table.Row>
42 </Table.Header>
43 <Table.Body>
44 {sorted.map((row) => (
45 <Table.Row key={row.name}>
46 <Table.Cell>{row.name}</Table.Cell>
47 <Table.Cell align="right">{row.score}</Table.Cell>
48 </Table.Row>
49 ))}
50 </Table.Body>
51 </Table>
52 )
53}
Get started

Install Drivn in one command

Copy the source into your project and own every line. Zero runtime dependencies, pure React + Tailwind.

npx drivn@latest create

Requires Node 18+. Works with npm, pnpm, and yarn.

Enjoying Drivn?
Star the repo on GitHub to follow new component releases.
Star →

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.