Skip to content

React Fragments: Cleaning Up the DIV Soup (Casual Guide)

Posted on:February 1, 2026 at 07:30 PM

Hello, Code Chefs! šŸ‘Øā€šŸ³

Have you ever opened your browser’s DevTools, inspected your React app, and stared in horror at the ā€œDiv Soupā€? You know what I mean. A mountain of nested <div> tags that look like this:

<div>
  <div>
    <div>
      <div>
        <h1>Hello World</h1>
      </div>
    </div>
  </div>
</div>

It’s ugly, it’s messy, and it’s not tasty. Today, we are going to fix this using a simple but powerful tool: React Fragments.

Grab your spoon, let’s clean up this soup! 🄣

The ā€œOne Parentā€ Rule

In React, a component’s render (or return statement) must return exactly one root element. You cannot return two siblings side-by-side.

This will cause an error:

// āŒ Error! React screams at you.
const Menu = () => {
  return (
    <h1>Home</h1>
    <h1>About</h1>
  );
};

React needs a single parent to wrap these elements. So, naturally, what do we do? We wrap it in a <div>.

// 😐 It works, but at what cost?
const Menu = () => {
  return (
    <div>
      <h1>Home</h1>
      <h1>About</h1>
    </div>
  );
};

Why the ā€œDiv Wrapperā€ is Bad

ā€So what? It’s just one extra div,ā€ you might say. Sure, one is fine. But imagine you have a Table component.

const TableRow = () => {
  return (
    <div>
      <td>Cell 1</td>
      <td>Cell 2</td>
    </div>
  );
};

// Usage
<table>
  <tr>
    <TableRow />
  </tr>
</table>

The resulting HTML will look like this:

<table>
  <tr>
    <div>  <!-- 😱 Invalid HTML! -->
      <td>Cell 1</td>
      <td>Cell 2</td>
    </div>
  </tr>
</table>

A <div> cannot be a direct child of a <tr>. This breaks your HTML structure and can ruin styling, especially if you are using Flexbox or Grid where parent-child relationships matter.

Enter: React Fragment

React saw this problem and gave us Fragments. A Fragment lets you group a list of children without adding extra nodes to the DOM. It’s like a Ghost Wrapper. šŸ‘»

import { Fragment } from 'react';

const Menu = () => {
  return (
    <Fragment>
      <h1>Home</h1>
      <h1>About</h1>
    </Fragment>
  );
};

When this renders in the browser, the Fragment disappears! You just get:

<h1>Home</h1>
<h1>About</h1>

Clean. Beautiful. Valid.

The Short Syntax: <> ... </>

Because developers are lazy (efficient!), there is a shorter way to write this. You don’t even need to import Fragment.

const Menu = () => {
  return (
    <>
      <h1>Home</h1>
      <h1>About</h1>
    </>
  );
};

This empty tag <> is syntactic sugar for React.Fragment. It does exactly the same thing.

When to Use the Full Name?

There is one specific case where you cannot use the short <> syntax: Loops with Keys.

If you are mapping over an array and returning a Fragment, you often need to pass a key prop. The short syntax does not support attributes.

const Glossary = ({ items }) => {
  return (
    <dl>
      {items.map(item => (
        // āœ… We need 'key', so we use Fragment explicitly
        <React.Fragment key={item.id}>
          <dt>{item.term}</dt>
          <dd>{item.description}</dd>
        </React.Fragment>
      ))}
    </dl>
  );
};

If you used <key={item.id}>, it would be a syntax error.

Conclusion

Fragments are one of those small features that make a huge difference in code quality.

  1. Cleaner DOM: No more useless divs.
  2. Valid HTML: No more div inside ul or tr.
  3. Better CSS: No more fighting with Flex/Grid layouts blocked by wrapper divs.

So next time you reach for that <div> just to satisfy React’s one-parent rule, stop! šŸ›‘ Use a Fragment <> instead.

Your code (and your browser) will thank you.

Happy Coding! šŸš€