Skip to main content
Fragments let you group a list of children without adding extra nodes to the DOM. This is useful when a component needs to return multiple elements but you don’t want to wrap them in an unnecessary container.

Fragment

The Fragment component allows you to group multiple children without adding an extra DOM element.

Type

Internally, Fragment is represented by the symbol Symbol.for('react.fragment') (the value of REACT_FRAGMENT_TYPE in React’s source).

Short Syntax

You can use <></> as shorthand for <Fragment></Fragment>:
The short syntax:
  • Is more concise and commonly used
  • Does not support keys or other attributes
  • Is functionally identical to <Fragment>

Usage

Returning Multiple Elements

Fragments are most commonly used to return multiple elements from a component:
Without fragments, you would need to wrap these in a <div> or other element, adding an extra node to the DOM:

Avoiding Wrapper Elements

Fragments are especially useful when wrapper elements would break styling or semantics: Table rows:
Flex/Grid layouts:
Lists:

With Text and Elements

Fragments can contain any mix of elements and text:

Conditional Rendering

Fragments work well with conditional rendering:

Using Keys with Fragments

When you need to provide a key to a fragment (e.g., when mapping over an array), you must use the explicit <Fragment> syntax:
The short syntax <></> does not support keys or any other attributes. If you need to add a key, you must use the explicit <Fragment key={...}> syntax.
Why keys are needed: When mapping over an array, React needs keys to track which items have changed:

When to Use Fragments

Use Fragments When:

1. You need to return multiple elements from a component:
2. Wrapper elements would break CSS or layout:
3. Semantic HTML requires specific parent-child relationships:
4. You’re mapping over data and each item needs multiple elements:

Don’t Use Fragments When:

1. You need to apply styles or event handlers:
2. You only have a single child:
3. The wrapper element has semantic meaning:

Fragment vs. Array

Before fragments existed, people used arrays to return multiple elements:
Advantages of fragments over arrays:
  • No need to add keys to every element
  • Cleaner, more readable syntax
  • Works better with TypeScript typing
  • No commas between elements

Example: Complex Fragment Usage

createElement with Fragments

You can create fragments programmatically using createElement:
With key:
Performance:Fragments have no performance overhead compared to using wrapper <div> elements. In fact, they can be slightly more performant because:
  1. They don’t create DOM nodes
  2. The React reconciliation algorithm has less work to do
  3. There are fewer elements in the DOM tree