TExcelTAD
Home › React › React Fragments

React Fragments

A component must return a single element. When you want to return several siblings without wrapping them in an extra <div>, use a Fragment. It groups children for React but leaves no trace in the DOM.

The problem: wrapper divs

Imagine a table. A component that renders a couple of cells can't wrap them in a <div> — <div> is not allowed inside <tr>, and the browser would break the table layout:

jsx
function Columns() {
  return (
    <div>            {/* ❌ invalid inside <tr> */}
      <td>Hello</td>
      <td>World</td>
    </div>
  );
}

The solution: <>…</>

The short syntax <> and </> creates a fragment:

jsx · try it yourself
function PriceCells({ net, vatRate }) {
  const vat = net * vatRate;
  return (
    <>
      <td>{net.toFixed(2)}</td>
      <td>{vat.toFixed(2)}</td>
      <td><strong>{(net + vat).toFixed(2)}</strong></td>
    </>
  );
}

export default function App() {
  return (
    <table border="1" cellPadding="6" style={{ borderCollapse: 'collapse' }}>
      <thead>
        <tr><th>Item</th><th>Net</th><th>VAT</th><th>Total</th></tr>
      </thead>
      <tbody>
        <tr><td>Laptop stand</td><PriceCells net={30} vatRate={0.1} /></tr>
        <tr><td>USB hub</td><PriceCells net={18.5} vatRate={0.1} /></tr>
      </tbody>
    </table>
  );
}

Inspect the table in DevTools: every <td> sits directly inside its <tr>. The fragment disappeared.

Fragments and layout

Extra wrappers also interfere with CSS layouts such as flexbox and grid, where only direct children are laid out. Compare the two versions:

jsx · try it yourself
function WithDiv() {
  return (
    <div>
      <div className="cell">B</div>
      <div className="cell">C</div>
    </div>
  );
}

function WithFragment() {
  return (
    <>
      <div className="cell">B</div>
      <div className="cell">C</div>
    </>
  );
}

const css = `
  .row { display: flex; gap: 8px; margin-bottom: 12px; }
  .cell { background: #61dafb; padding: 12px 20px; border-radius: 6px; font-weight: bold; }
`;

export default function App() {
  return (
    <div>
      <style>{css}</style>
      <p>With a wrapper div — B and C are stacked inside one flex item:</p>
      <div className="row"><div className="cell">A</div><WithDiv /></div>
      <p>With a fragment — B and C are real flex items:</p>
      <div className="row"><div className="cell">A</div><WithFragment /></div>
    </div>
  );
}

Keyed fragments

The short syntax can't take attributes. When you render fragments in a list, you need a key, so use the long form <Fragment key={…}>:

jsx · try it yourself
import { Fragment } from 'react';

const glossary = [
  { term: 'JSX', definition: 'HTML-like syntax that compiles to JavaScript.' },
  { term: 'Props', definition: 'Inputs passed from a parent component.' },
  { term: 'State', definition: 'Data a component remembers between renders.' },
];

export default function App() {
  return (
    <dl>
      {glossary.map((item) => (
        <Fragment key={item.term}>
          <dt style={{ fontWeight: 'bold', marginTop: 8 }}>{item.term}</dt>
          <dd>{item.definition}</dd>
        </Fragment>
      ))}
    </dl>
  );
}

A <dl> must contain <dt>/<dd> pairs directly, so a wrapper <div> per pair would produce awkward markup — the keyed fragment is exactly right.

Fragment syntax summary

Syntax Key allowed? Import needed? Use when
<>…</> no no most cases
<Fragment>…</Fragment> yes import { Fragment } from 'react' inside .map() lists
<React.Fragment>…</React.Fragment> yes import React from 'react' older code style

Fragments vs. arrays

You could also return an array of elements, but each would need a key and the syntax is noisier. Fragments are the idiomatic choice:

jsx
// Works, but awkward
return [<h1 key="t">Title</h1>, <p key="s">Subtitle</p>];

// Preferred
return (
  <>
    <h1>Title</h1>
    <p>Subtitle</p>
  </>
);

Tip: Don't add fragments "just in case". If you only return one element, you don't need one. Use fragments when you'd otherwise add a meaningless wrapper.

Test yourself

What does a Fragment add to the DOM?

  1. A <div>
  2. A <span>
  3. Nothing
  4. A <template> element

You render pairs of <dt>/<dd> in a map and need a key. What do you write?

  1. <key={id}>…</>
  2. <Fragment key={id}>…</Fragment>
  3. <> key={id} …</>
  4. A <div key={id}>

Why can wrapper divs break a flexbox layout?

  1. Flexbox doesn't support divs
  2. Only direct children of the flex container become flex items
  3. Divs are block elements and ignore flex
  4. React removes CSS from divs