React Reference ยท Built-in DOM Components
Tables in React (table, tr, td)
Reference for table markup in React JSX: thead, tbody and tfoot, keys on rows, colSpan and rowSpan, accessible headers, and the validateDOMNesting warnings.
HTML tables work in React exactly as they do in plain HTML, with two differences you will notice quickly: attributes are camelCase (colSpan, rowSpan), and React warns loudly when the markup would not survive the browser's parser. A table built with <table>, <thead>, <tbody>, <tr>, <th> and <td> is the right element for data that has rows and columns, and it comes with built-in accessibility for screen readers.
Tables are usually generated from arrays: map the data to rows and give each row a stable key. Keep the structure strict. A <tr> belongs inside <thead>, <tbody> or <tfoot>, and only <td> and <th> belong inside a <tr>.
<table> <caption>Monthly sales</caption> <thead> <tr><th scope="col">Month</th><th scope="col">Units</th></tr> </thead> <tbody> {rows.map((r) => <tr key={r.month}><td>{r.month}</td><td>{r.units}</td></tr>)} </tbody> </table>
Try it
Syntax
<table> <caption /> <colgroup><col /></colgroup> <thead><tr><th /></tr></thead> <tbody><tr><td /></tr></tbody> <tfoot><tr><td /></tr></tfoot> </table>
| Name | Description |
|---|---|
colSpan, rowSpan |
Number of columns or rows a cell covers. Pass numbers, such as colSpan={2}. The lowercase colspan is not recognized by React and produces a warning. |
headers, scope |
Link a cell to its header (scope="col", scope="row") so assistive technology can announce headings. |
span |
On <col> and <colgroup>: how many columns the element styles. |
cellPadding, cellSpacing, border |
Legacy presentation attributes. Prefer CSS; they are shown in the examples only for a visible grid. |
key |
Required on each element produced by .map(): the row (or fragment holding several rows). |
Returns: table elements. React adds nothing to them.
Errors: in development React logs validateDOMNesting style warnings, such as a <tr> directly inside <table>, a <td> outside a <tr>, a <div> directly inside <table>, or text directly inside <tr>. During hydration a mismatch like this can become a real error, because the browser's parser moves misplaced nodes before React sees them.
Usage notes
- Always include
<tbody>. Browsers insert one automatically when parsing HTML, so a React tree with<tr>directly under<table>differs from the DOM that server-rendered HTML produces. Writing<tbody>yourself avoids the mismatch. - No wrapper
<div>around rows. To group rows, use a Fragment with a key:<Fragment key={id}>...</Fragment>, or several<tbody>elements, which HTML allows. - Whitespace text is ignored by React. Newlines and indentation between table tags do not create text nodes, but a stray
{' '}between<tr>elements does and triggers a warning. - Cells must be inside rows. Conditional rendering such as
{cond && <td />}is fine, as long as every row ends up with consistent column counts, or the table will look ragged. - Do not use tables for page layout. Use CSS grid or flexbox. Layout tables confuse screen readers.
- Scrolling wide tables. Wrap the
<table>in a<div style={{ overflowX: 'auto' }}>; the wrapper goes outside the table, which is allowed. - Large tables. Thousands of rows are slow to render. Paginate or virtualize them.
- Sorting. Sort a copy of the array (
[...rows].sort(...)) so you never mutate state, and keep keys tied to the data, not to the index.
Related
key,<Fragment>,style,className- Lessons: Lists and keys, Virtualization