React Fragments

Group elements without extra DOM nodes

🧩 What are Fragments?

Fragments let you group multiple elements without adding extra nodes to the DOM. They're perfect when you need to return multiple elements from a component without wrapping them in unnecessary divs.


<Fragment>
  <h1>Title</h1>
  <p>Content</p>
</Fragment>
                                    

Key Concepts

🎯

Clean DOM

No extra wrapper elements

📦

Grouping

Return multiple elements

Performance

Lighter DOM structure

Syntax

Short syntax available

🔹 Problem Without Fragments

Without fragments, you must wrap elements in a div, adding unnecessary nodes to the DOM. This can break CSS layouts, especially with flexbox or grid, and makes the DOM tree unnecessarily deep.

// ❌ Extra div wrapper
function TableRow() {
  return (
    <div>  {/* Unnecessary wrapper */}
      <td>Cell 1</td>
      <td>Cell 2</td>
    </div>
  );
}

// This breaks table structure!

Problem:

Extra div breaks table layout and adds unnecessary DOM nodes.

🔹 Solution with Fragments

Fragments solve this by grouping elements without adding DOM nodes. Your component can return multiple elements that render as siblings in the parent, maintaining proper HTML structure.

import { Fragment } from 'react';

// ✅ Using Fragment
function TableRow() {
  return (
    <Fragment>
      <td>Cell 1</td>
      <td>Cell 2</td>
    </Fragment>
  );
}

// Usage in table
function Table() {
  return (
    <table>
      <tbody>
        <tr>
          <TableRow />
        </tr>
      </tbody>
    </table>
  );
}

Output:

Cell 1 Cell 2

🔹 Short Syntax

Use the short syntax <> </> for cleaner code. This is the most common way to use fragments and requires no import statement, making your code more concise.

// Short syntax (most common)
function UserInfo() {
  return (
    <>
      <h2>John Doe</h2>
      <p>Software Developer</p>
      <p>[email protected]</p>
    </>
  );
}

Output:

John Doe

Software Developer

[email protected]

🔹 Fragments with Keys

When mapping over arrays, use the full Fragment syntax to add keys. The short syntax doesn't support keys, so you must use React.Fragment for lists.

import { Fragment } from 'react';

function ItemList({ items }) {
  return (
    <dl>
      {items.map(item => (
        <Fragment key={item.id}>
          <dt>{item.term}</dt>
          <dd>{item.definition}</dd>
        </Fragment>
      ))}
    </dl>
  );
}

// Usage
const glossary = [
  { id: 1, term: 'React', definition: 'A JavaScript library' },
  { id: 2, term: 'JSX', definition: 'JavaScript XML syntax' }
];

Output:

React
A JavaScript library
JSX
JavaScript XML syntax

🔹 Common Use Cases

Fragments are essential in several scenarios:

// 1. Returning multiple elements
function Header() {
  return (
    <>
      <h1>Welcome</h1>
      <nav>Navigation</nav>
    </>
  );
}

// 2. Conditional rendering
function Message({ show }) {
  return (
    <>
      {show && (
        <>
          <h3>Alert!</h3>
          <p>Important message</p>
        </>
      )}
    </>
  );
}

// 3. List items
function List() {
  return (
    <ul>
      <>
        <li>Item 1</li>
        <li>Item 2</li>
      </>
    </ul>
  );
}

🔹 Best Practices

✅ Do:

  • Use short syntax <> </> when possible
  • Use Fragment with key prop in lists
  • Keep DOM structure clean
  • Use for table rows and list items

❌ Don't:

  • Add unnecessary div wrappers
  • Use short syntax when you need keys
  • Nest fragments unnecessarily
  • Use fragments when a semantic element is better

🧠 Test Your Knowledge

What is the short syntax for Fragment?