A Fragment (<>…</> or <Fragment>…</Fragment>) lets a component return multiple elements without adding an extra DOM node. This matters because a component returns a single value, and wrapper <div>s can break HTML semantics (tables, lists, definition lists), break CSS layouts (flex/grid children), and add DOM bloat. Use the long form <Fragment key={…}> when you need a key in a mapped list.
function Columns() {
return (
<>
<td>Name</td>
<td>Email</td>
</>
);
}
function Table() {
return (
<table>
<tbody>
<tr><Columns /></tr>
</tbody>
</table>
);
}
A <div> around the <td>s would produce invalid HTML (<tr><div><td>). Browsers repair it unpredictably, and React warns about invalid nesting (which also causes hydration problems).
import { Fragment } from 'react';
function Glossary({ terms }) {
return (
<dl>
{terms.map(t => (
<Fragment key={t.id}>
<dt>{t.word}</dt>
<dd>{t.definition}</dd>
</Fragment>
))}
</dl>
);
}
<dl> expects <dt>/<dd> pairs as direct children, so a keyed fragment is the only clean way to emit pairs. The short syntax <> can't take key or any other attribute.
<ul> → <li>, <table> → <tr> → <td>, <select> → <option>, and <dl> all have strict content models.display: flex or grid containers, an extra wrapper becomes the flex/grid item and breaks the layout. (display: contents is a CSS workaround but has had accessibility bugs in some browsers.):first-child/> selector breakages.A Fragment has no DOM node but still matters for reconciliation. React special-cases one level of fragment: switching between <Child />, <><Child /></> and [<Child />] keeps Child's state. Go deeper (<><><Child /></></> vs <Child />) and the tree shape no longer matches, so Child remounts and loses its state. Keep the structure consistent between conditional branches.
return [<li key="a" />, <li key="b" />]), but they need keys and read worse than a fragment.key (and, in canary/experimental builds, a ref for fragment refs). There's no className or event handlers, because there's no DOM node.<> doesn't need React in scope.<Fragment> in lists (and remember <> can't take a key).<dl> pairs, flex/grid children.Spotted a mistake or a rights concern? Report an issue