Sub topic 7
Keys on Fragments
This chapter assumes you can write function components, pass props, and render a list with map. You should also know why each list item needs a key. Here you will learn what to do when one list item must render more than one element.
The problem: two elements per item
A component must return a single root. Sometimes a list item needs two or more sibling elements. A description list (<dl>) is a common case. Each entry is a <dt> (term) followed by a <dd> (definition), and they must be direct children of the <dl>.
Wrapping each pair in a <div> would produce invalid HTML inside a <dl>, and it can break your styling. A Fragment groups elements without adding a node to the DOM.
Why the short syntax fails
You may already use the short Fragment syntax, <>...</>. It is convenient, but it cannot take any attributes, including key. Inside map, React needs a key on the outermost element that each callback returns. If that element is a short Fragment, you cannot supply one.
You will see a warning like this in the console:
Warning: Each child in a list should have a unique "key" prop.The fix: import Fragment and key it
Import Fragment from React and write it out in full. The full form accepts a key.
import { Fragment } from 'react';
function Glossary({ entries }) {
return (
<dl>
{entries.map(entry => (
<Fragment key={entry.id}>
<dt>{entry.term}</dt>
<dd>{entry.definition}</dd>
</Fragment>
))}
</dl>
);
}The key goes on the Fragment, not on the <dt> or <dd>. The Fragment is the element returned by the callback, so it is the one React tracks between renders. The children inside it do not need keys.
Common errors
- Using
<>insidemap. This triggers the missing-key warning. Switch to<Fragment key={...}>. - Putting the key on a child. A key on the
<dt>does not satisfy React. The key must be on the outermost element returned from the callback. - Keying by index. The same rule applies as for any list. Use a stable id from your data, not the array index, when items can be reordered, inserted, or removed.
- Forgetting the import.
React.Fragmentalso works if you importReact. Otherwise import{ Fragment }as shown above.
Fragment or wrapper element?
| Situation | Use |
|---|---|
| Siblings must stay direct children of the parent (`dl`, `table`, `tr`) | Keyed `Fragment` |
| The wrapper needs a class, style, or event handler | A real element such as `div` |
| You can extract the item into its own component | A component with the `key` on its usage |
The last row also works well. If you extract an Entry component that returns a short Fragment, put the key where you use it: <Entry key={entry.id} entry={entry} />. The key is then on the component element, which is what map returns.
Exercise
A teammate writes entries.map(entry => (<><dt>{entry.term}</dt><dd>{entry.definition}</dd></>)) and sees a missing-key warning. Why can't they fix it by adding key to the <dt>, and what should they change?
Show answer
The callback returns the short Fragment <>...</>, so that Fragment is the element React tracks, and it needs the key. The short syntax cannot take a key. They should import Fragment from React and write <Fragment key={entry.id}> ... </Fragment>, using a stable id from the data.