What You'll Learn
You'll learn when and how to add key props to React Fragments when rendering lists.
The Problem
You've learned that Fragments (<>...</>) let you group elements without adding extra DOM nodes. But there's a catch: the shorthand syntax cannot accept props, including the key prop.
When you're mapping over an array and need to return multiple elements per item, you'll need keys—but <> won't work:
{items.map(item => (
<> {/* ❌ Can't add key here! */}
<dt>{item.term}</dt>
<dd>{item.definition}</dd>
</>
))}
The Solution: Long-Form Fragment Syntax
Use the explicit <Fragment> component instead of the shorthand. It accepts the key prop:
import { Fragment } from 'react';
{items.map(item => (
<Fragment key={item.id}>
<dt>{item.term}</dt>
<dd>{item.definition}</dd>
</Fragment>
))}
This gives React the unique identifier it needs to track each group of elements, while still avoiding unnecessary wrapper divs in the DOM.
When You Need This
- Mapping arrays where each item renders multiple sibling elements
- Definition lists (
<dt>and<dd>pairs) - Table rows with multiple cells that need semantic grouping
- Any time
<div>would break HTML validity or styling
The Rule
If you're mapping and need a Fragment, use <Fragment key={...}>. If you're not mapping, the <> shorthand is fine.
Key Takeaway: When rendering Fragments in .map(), you must use the long-form <Fragment key={...}> syntax—the <> shorthand cannot accept props.