Master React.Fragment, short syntax, and when keys are necessary.
function List() {
return (
<React.Fragment>
<h2>Title</h2>
<p>Description</p>
</React.Fragment>
);
}// Without Fragment: creates a div
<div>
<Header />
<Content />
<Footer />
</div>
// With Fragment: no extra node
<React.Fragment>
<Header />
<Content />
<Footer />
</React.Fragment>function Profile() {
return (
<React.Fragment>
<UserHeader />
<UserContent />
<UserFooter />
</React.Fragment>
);
}function TableRow({ item }) {
return (
<React.Fragment>
<td>{item.name}</td>
<td>{item.price}</td>
<td>{item.stock}</td>
</React.Fragment>
);
}// JSX requires a single root — Fragment is the clean solution
function NavItems() {
return (
<>
<li><a href="/home">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</>
);
}function List() {
return (
<>
<h2>Title</h2>
<p>Description</p>
</>
);
}// Preferred: short syntax
<>
<Component />
</>
// Also works: full syntax
<React.Fragment>
<Component />
</React.Fragment>function App() {
return (
<main>
<>
<Sidebar />
<Content />
</>
</main>
);
}function Status({ isLoggedIn }) {
return isLoggedIn ? (
<>
<WelcomeMessage />
<UserMenu />
</>
) : (
<>
<LoginPrompt />
<SignupLink />
</>
);
}function List({ items }) {
return (
<>
{items.map(item => (
<React.Fragment key={item.id}>
<dt>{item.name}</dt>
<dd>{item.description}</dd>
</React.Fragment>
))}
</>
);
}// Without key: React can't track which elements belong together
items.map(item => (
<React.Fragment key={item.id}>
<Header title={item.title} />
<Body content={item.content} />
</React.Fragment>
))// This won't work:
<>
{items.map(item => (
<>
<p key={item.id}>{item.name}</p>
</>
))}
</>// Wrong: key on child, not on fragment
{items.map(item => (
<React.Fragment>
<p key={item.id}>{item.name}</p>
</React.Fragment>
))}
// Correct: key on the fragment itself
{items.map(item => (
<React.Fragment key={item.id}>
<p>{item.name}</p>
</React.Fragment>
))}// These keys are fine — they're in separate lists
listA.map(item => <React.Fragment key={item.id}>...</React.Fragment>)
listB.map(item => <React.Fragment key={item.id}>...</React.Fragment>)function DialogBox() {
return (
<>
<Modal.Header>Confirm Action</Modal.Header>
<Modal.Body>Are you sure?</Modal.Body>
<Modal.Footer>
<button>Cancel</button>
<button>Confirm</button>
</Modal.Footer>
</>
);
}return (
<>
<style>{`
span { color: red; }
`}</style>
<span>Styled text</span>
</>
);function DefinitionList({ terms }) {
return (
<dl>
{terms.map(term => (
<React.Fragment key={term.id}>
<dt>{term.word}</dt>
<dd>{term.definition}</dd>
</React.Fragment>
))}
</dl>
);
}function ProductRow({ product }) {
return (
<tr>
<React.Fragment>
<td>{product.name}</td>
<td>{product.category}</td>
<td>${product.price}</td>
</React.Fragment>
</tr>
);
}function NavGroup({ label, links }) {
return (
<>
<li className="group-label">{label}</li>
{links.map(link => (
<li key={link.href}>
<a href={link.href}>{link.text}</a>
</li>
))}
</>
);
}// Wrong: loses key benefit
{items.map(item => (
<>
<p key={item.id}>{item.name}</p>
</>
))}
// Correct: use React.Fragment with key
{items.map(item => (
<React.Fragment key={item.id}>
<p>{item.name}</p>
</React.Fragment>
))}// Wrong: fragments don't support attributes
<> className="wrapper">
<Content />
</>
// Correct: use a div if you need attributes
<div className="wrapper">
<Content />
</div>// Required for React.Fragment in older setups
import React from "react";
// Not required for short syntax — handled by JSX transform
<>...</>// Bad: index as key causes issues when list order changes
{items.map((item, index) => (
<React.Fragment key={index}>...</React.Fragment>
))}
// Good: stable unique ID
{items.map(item => (
<React.Fragment key={item.id}>...</React.Fragment>
))}// Unnecessary nesting
<>
<>
<p>Hello</p>
</>
</>
// Clean: single fragment
<>
<p>Hello</p>
</>Use React.Fragment when you need to pass a key prop, such as when rendering a list of fragments. The short syntax <></> doesn't accept any props, so explicit React.Fragment is the only option in those cases.
No, Fragments don't support any DOM attributes like className, id, or style. If you need to style the wrapper element, use a div or another semantic HTML element instead.
Map over your data and return React.Fragment with a key prop for each item: items.map(item => <React.Fragment key={item.id}>...</React.Fragment>). This avoids wrapper divs while satisfying React's key requirement for lists.
No, Fragments are invisible in the DOM — they leave no trace in the rendered HTML. Your flex or grid container will see the Fragment's children as direct children, which is exactly why Fragments are useful for avoiding layout-breaking wrapper divs.
Fragments themselves don't affect render order, so if children appear out of order the issue is elsewhere — typically conditional rendering logic or an unsorted data source. Check the order of expressions inside your Fragment and the data being mapped.