Skip to content

Fragments

Grouping elements without adding extra DOM nodes using React Fragments.

What you'll learn

  • Why fragments exist and the problem they solve
  • Using the shorthand <></> syntax
  • Using React.Fragment with a key prop
  • When fragments matter for CSS and semantics

Concept

The Extra DOM Node Problem

React components must return a single root element. Before fragments, the common workaround was to wrap sibling elements in a <div>. This added unnecessary DOM nodes that could break CSS layouts (like CSS Grid or Flexbox, which depend on direct children), invalidate HTML semantics (like <li> inside <ul>, which cannot have a wrapping <div>), and slow down rendering with extra DOM nodes. Fragments solve this by letting you group elements without adding a DOM node.

The Shorthand Syntax

The most common fragment syntax is the shorthand: <></>. It is empty tags that wrap multiple elements without rendering anything to the DOM. It is concise and readable. However, the shorthand does not support attributes — most importantly, it cannot take a key prop. When you need a key (in a list), you must use the explicit <React.Fragment> form.

React.Fragment with key

When rendering a list of fragments, each fragment needs a key prop just like any other list item. The shorthand <></> cannot accept a key, so you must use <React.Fragment key={item.id}>. This is common when a list item needs to return multiple sibling elements, like a table row with multiple cells or a definition list with terms and descriptions.

When Fragments Matter

Fragments matter most when the DOM structure is important: CSS Grid and Flexbox layouts that rely on direct children, HTML tables (<tr> cannot contain a <div>), definition lists, and any case where an extra <div> would break the HTML structure or CSS selectors. They also matter for performance — every DOM node has a cost, and eliminating unnecessary wrappers can measurably speed up large lists.

Fragments vs Arrays

Before fragments existed, developers returned arrays of elements from components. This still works but requires explicit keys on each element and is less readable. Fragments are the modern, preferred approach — they do not require keys (unless in a list) and are more expressive. Always prefer fragments over array returns for grouping sibling elements.

Example

              import React, { Fragment } from "react";

// Shorthand fragment — no extra DOM node.
function Header() {
  return (
    <>
      <h1>Page Title</h1>
      <p>Subtitle text</p>
    </>
  );
}

// Explicit Fragment with key — needed in lists.
function DefinitionList({ terms }) {
  return (
    <dl>
      {terms.map(term => (
        <Fragment key={term.id}>
          <dt>{term.name}</dt>
          <dd>{term.description}</dd>
        </Fragment>
      ))}
    </dl>
  );
}

// Without fragments, this would need a wrapping <div> that breaks <dl> semantics.
function App() {
  const terms = [
    { id: 1, name: "React", description: "A UI library" },
    { id: 2, name: "Fragment", description: "A node-less wrapper" },
  ];
  return (
    <>
      <Header />
      <DefinitionList terms={terms} />
    </>
  );
}
            

The shorthand <> groups Header's elements. The explicit Fragment with key lets DefinitionList return dt/dd pairs without a wrapper div, preserving dl semantics.

Try it

Common mistakes

The mistake

Using the shorthand <></> when you need a key prop

The fix

The shorthand syntax cannot accept any attributes. When you need a key (in lists), use the explicit <React.Fragment key={id}> form instead of <></>.

The mistake

Wrapping list items in unnecessary <div> elements

The fix

Use fragments instead of divs when grouping siblings. This avoids extra DOM nodes that can break CSS layouts and HTML semantics, especially in tables and lists.

Practice Fragments

6 exercises

Practice Fragments

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors