Sub topic 5
Returning multiple elements with fragments
This chapter assumes you can write a function component that returns JSX, embed expressions in curly braces, and pass props. It also assumes you know basic HTML, including that <dl> lists contain <dt> and <dd> children.
The single-root rule
A component returns one value. JSX that looks like two siblings is really two separate expressions, so this fails to compile:
function Row({ label, value }) {
return (
<dt>{label}</dt>
<dd>{value}</dd>
);
}The error is usually "Adjacent JSX elements must be wrapped in an enclosing tag." The component needs exactly one root.
The wrapper problem
The obvious fix is a <div>. That often produces invalid or broken HTML. A <dl> may contain only <dt> and <dd> elements (and <div> groups), so an extra wrapper inside a row component can break the structure. The same problem appears with <tr> inside <table> and <li> inside <ul>. Extra wrappers can also break CSS layouts such as flexbox and grid, because the wrapper becomes the child that the layout applies to.
Fragments
A fragment groups children without adding a node to the DOM. The short syntax is an empty tag pair:
function Row({ label, value }) {
return (
<>
<dt>{label}</dt>
<dd>{value}</dd>
</>
);
}Use it inside a list:
function Details() {
return (
<dl>
<Row label="Name" value="Ada" />
<Row label="Field" value="Mathematics" />
</dl>
);
}The rendered HTML is a <dl> containing four direct children and no wrapper elements.
The diagram shows the component tree. In the DOM, the two Row nodes do not exist, so the dt and dd elements sit directly under the dl.
The keyed form: `<Fragment>`
The <> shorthand cannot take any attributes. When you render a list and each item produces several elements, you need a key on the outer element of each item. Import Fragment and use its long form:
import { Fragment } from 'react';
function Glossary({ terms }) {
return (
<dl>
{terms.map((term) => (
<Fragment key={term.id}>
<dt>{term.name}</dt>
<dd>{term.definition}</dd>
</Fragment>
))}
</dl>
);
}Without the key, React warns that each child in a list needs a unique key. The next chapters on lists cover how to choose keys. Here, the point is that <Fragment key=...> is the only fragment form that accepts one.
Common errors
- Using
<>with a key.<key={id}>is a syntax error. Switch to<Fragment key={id}>. - Forgetting the import for
Fragment. The shorthand needs no import with the modern JSX transform. The named form does, unless you writeReact.Fragment. - Reaching for a
<div>by habit. Check whether the extra element changes the HTML validity or layout. If it does, use a fragment. - Mismatched closing tags.
<>must close with</>, and<Fragment>with</Fragment>.
Returning an array
A component can also return an array of elements, as Glossary effectively does inside the dl. Each element in an array needs a key. In most code, a fragment is clearer than returning a bare array because you control the structure explicitly.
Exercise
A TableRow component renders two <td> cells for each item in a rows array, inside a <tbody>. Which fragment form do you use for each item in rows.map(...), and why?
Show answer
Use <Fragment key={row.id}>, imported from 'react'. A wrapper <div> would be invalid inside <tbody>, so you need a fragment. The <> shorthand cannot carry a key, and items in a mapped list need one.
Exercise
Why does <><dt>Name</dt><dd>Ada</dd></> work as a return value when <dt>Name</dt><dd>Ada</dd> does not?
Show answer
A function returns one value, so JSX needs a single root expression. The fragment is that single root. At render time React places its children directly in the parent without creating a DOM node.