Skip to main content
Fragments let you group a list of children without adding extra nodes to the DOM. They’re useful when you need to return multiple elements from a component but don’t want to wrap them in a container element.

Why Use Fragments?

Without fragments, you’d need to wrap multiple elements in a container:
This extra <div> can cause issues with:
  • CSS layouts (flexbox, grid)
  • Table structures
  • List semantics
  • Extra DOM nodes affecting performance

Using Fragments

How Fragments Work

The Fragment component is implemented simply in src/create-element.js:
It just returns its children directly, without wrapping them. During diffing, Preact handles fragments specially to avoid creating wrapper DOM nodes. Reference: src/create-element.js:77-79

Common Use Cases

Keyed Fragments

When rendering a list of fragments, you need to provide a key prop. The short syntax <> doesn’t support keys, so use the explicit Fragment component:
When mapping over fragments, always use <Fragment key={...}> instead of <>.

Fragments vs. Arrays

You might wonder: why use fragments when you can return an array?

Practical Examples

Best Practices

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