Why Use Fragments?
Without fragments, you’d need to wrap multiple elements in a container:<div> can cause issues with:
- CSS layouts (flexbox, grid)
- Table structures
- List semantics
- Extra DOM nodes affecting performance
Using Fragments
- JSX Syntax (Recommended)
- Explicit Fragment
The most common way to use fragments is with the short syntax This renders the children without any wrapper element in the DOM.
<>...</>:How Fragments Work
TheFragment component is implemented simply in src/create-element.js:
src/create-element.js:77-79
Common Use Cases
Keyed Fragments
When rendering a list of fragments, you need to provide akey prop. The short syntax <> doesn’t support keys, so use the explicit Fragment component:
import { Fragment } from 'preact';
function Blog({ posts }) {
return (
<div>
{posts.map(post => (
<Fragment key={post.id}>
<h2>{post.title}</h2>
<p>{post.excerpt}</p>
<hr />
</Fragment>
))}
</div>
);
}
import { Fragment } from 'preact';
function TwoColumnList({ items }) {
return (
<div className="two-columns">
{items.map(item => (
<Fragment key={item.id}>
<div className="column-a">{item.title}</div>
<div className="column-b">{item.content}</div>
</Fragment>
))}
</div>
);
}
Fragments vs. Arrays
You might wonder: why use fragments when you can return an array?- With Fragments (Recommended)
- With Arrays
- More readable JSX syntax
- No need for keys on static elements
- Consistent with React
Practical Examples
Best Practices
// Good: Short syntax for simple cases
function Component() {
return (
<>
<Header />
<Main />
</>
);
}
// Only use explicit Fragment when you need keys
import { Fragment } from 'preact';
function Component({ items }) {
return items.map(item => (
<Fragment key={item.id}>
<div>{item.title}</div>
<div>{item.content}</div>
</Fragment>
));
}
// Bad: Unnecessary wrapper
function Component() {
return (
<div>
<Header />
</div>
);
}
// Good: Return directly if single element
function Component() {
return <Header />;
}
// Good: Use fragment for multiple elements
function Component() {
return (
<>
<Header />
<Main />
</>
);
}
// Not always best: Fragment in navigation
function Nav({ items }) {
return (
<nav>
{items.map(item => (
<>
<a href={item.href}>{item.label}</a>
<span> | </span>
</>
))}
</nav>
);
}
// Better: Use a list for semantics
function Nav({ items }) {
return (
<nav>
<ul>
{items.map(item => (
<li key={item.href}>
<a href={item.href}>{item.label}</a>
</li>
))}
</ul>
</nav>
);
}
import { Fragment } from 'preact';
// Bad: Missing keys
function Component({ items }) {
return items.map(item => (
<>
<h3>{item.title}</h3>
<p>{item.text}</p>
</>
));
}
// Good: Keyed fragments
function Component({ items }) {
return items.map(item => (
<Fragment key={item.id}>
<h3>{item.title}</h3>
<p>{item.text}</p>
</Fragment>
));
}
Performance Considerations
Fragments have no performance overhead - they don’t create extra DOM nodes or components. They’re just a syntax feature that tells Preact to render children directly.Next Steps
Server-Side Rendering
Render Preact components on the server
TypeScript
Use Preact with TypeScript for type safety