Learn the concept
JSX
React Fragments let you group multiple elements without adding an extra DOM node. Use the <Fragment> syntax when you need to pass a key prop, or the shorter <>...</> syntax when you don't.
React requires every component to return a single root element. Before Fragments, developers wrapped sibling elements in a <div>, which added unnecessary nodes to the DOM. Fragments solve this by grouping children without producing any DOM output.
Without Fragments, returning multiple elements requires a wrapper:
// This adds an unnecessary <div> to the DOM
function Columns() {
return (
<div>
<td>Hello</td>
<td>World</td>
</div>
);
}This <div> inside a <tr> creates invalid HTML and breaks table layout. Fragments eliminate the wrapper:
function Columns() {
return (
<>
<td>Hello</td>
<td>World</td>
</>
);
}<>...</> — concise, no attributes allowed:<><h1>Title</h1><p>Content</p></><Fragment> syntax — required when passing a key prop:import { Fragment } from 'react';
items.map((item) => (
<Fragment key={item.id}>
<dt>{item.term}</dt>
<dd>{item.description}</dd>
</Fragment>
));<td> elements from a component without breaking <table> structure<dt> + <dd> pairs<div> that becomes an unwanted flex/grid child and breaks layout<Fragment key={id}> when mapping over arrays and returning multiple elements per itemkey)<div> is semantically meaningful or needed for layout — use the appropriate HTML element insteadYou can also return arrays from components: return [<li key="1">A</li>, <li key="2">B</li>]. However, Fragments are preferred because:
key prop required (unless using <Fragment key>)import { Fragment } from 'react';
// Short syntax — most common
function Header() {
return (
<>
<h1>Welcome</h1>
<p>Description text</p>
</>
);
}
// Explicit Fragment with key — required for lists
function Glossary({ items }) {
return (
<dl>
{items.map((item) => (
<Fragment key={item.id}>
<dt>{item.term}</dt>
<dd>{item.definition}</dd>
</Fragment>
))}
</dl>
);
}
// Table example — div wrapper would break HTML
function TableRow({ data }) {
return (
<>
<td>{data.name}</td>
<td>{data.value}</td>
<td>{data.status}</td>
</>
);
}Components that return table cells (<td>) must use Fragments to avoid breaking the table structure with invalid wrapper elements.
Headless UI components return Fragments to avoid adding DOM nodes that interfere with consumer styling and layout.
Build form field components that return label + input + error message using Fragments, ensuring no extra wrappers break the form layout.