Menu

React Fragment: <>...</> vs <Fragment> with Examples

A React Fragment groups several elements into one return value without adding a node to the page. Learn the short <>...</> syntax, when you need <Fragment key>, and why fragments matter inside tables and lists.

This page includes runnable editors - edit, run, and see output instantly.

A React Fragment lets a component return several elements without wrapping them in an extra <div>. Write <> before the first element and </> after the last one, and React puts the elements straight into the parent, with no node of its own in the page.

Each Greeting returns a heading and a paragraph side by side. Delete the <> and </> lines and the preview shows a build error instead, because the function now tries to return two values.

Why a component needs one root

JSX is a nicer way to write function calls. <h2>Hi</h2> compiles to roughly jsx('h2', { children: 'Hi' }), one expression. A return statement can hand back one value, so two sibling tags with nothing around them are like writing return a b: the compiler stops with "Adjacent JSX elements must be wrapped in an enclosing tag".

Before fragments existed, the fix was a wrapper <div>. That works, but the div is a real element. It shows up in the DOM, it takes part in CSS layout, and some parents do not accept it as a child at all. A fragment satisfies the one root rule and then disappears.

You can see that it adds nothing. The example below renders the same two items through a fragment and through a div, then counts the direct children of each container.

The console prints 2 for the fragment, the two spans, and 1 for the div, which wraps them. In a flex or grid container that difference decides whether the spans are laid out as items or bundled into one.

Short syntax and the Fragment component

There are two ways to write a fragment, and they compile to the same thing:

import { Fragment } from 'react';

// Short syntax, no import needed
<>
    <dt>React</dt>
    <dd>A library for building user interfaces</dd>
</>

// Long form
<Fragment>
    <dt>React</dt>
    <dd>A library for building user interfaces</dd>
</Fragment>

Use <>...</> almost everywhere. Reach for <Fragment> in one case: when the fragment needs a key. The short syntax cannot take attributes, so <key={id}> is not valid JSX. You may also see <React.Fragment> in older code; it is the same component reached through the default React import.

Keyed fragments in lists

When you render a list with map, every item needs a key so React can track it between renders (the page on lists and keys explains why). If each item is a group of elements instead of one, the key goes on the fragment, so you need the long form.

A description list is the classic case: each entry is a <dt> and a <dd>, and a <dl> should contain those directly.

Click Reverse and the pairs swap places as whole units, because each pair has its own key. A wrapper <div> around each pair would also take a key, and it is valid HTML inside a dl, but it puts a third level into the markup that a fragment keeps out.

Fragments inside tables

Tables are where a stray wrapper hurts most. A <tr> expects <td> and <th> cells as its direct children. If a component that returns several cells wraps them in a <div>, the browser treats the div as one odd cell and the cells inside it stop lining up with the header.

The fragment row has its price under Price and its stock under Stock. In the div row both cells are squeezed into the Price column and the Stock column is empty. In development React also logs a warning that a <div> cannot be a child of a <tr>; the preview runs like a production build, so it skips that check, and the broken layout is what you see here. With server rendering the same mistake can also cause hydration errors, because the browser's HTML parser moves the misplaced div.

The same rule applies to <ul> and <ol>, which should contain <li> elements, and to <select>, which should contain <option> elements. Whenever a component returns pieces that belong directly inside such a parent, return a fragment.

Fragments or arrays

A component can also return an array of elements, and React renders them in order like a fragment. Each element in a returned array needs its own key, just like the output of map, because React treats the array as a list:

function Cells() {
    return [<td key="price">$4</td>, <td key="stock">In stock</td>];
}

This works, but it is noisier: commas between elements, a key on each one, and a bracket that is easy to miss when the markup grows. Use a fragment for a fixed group of elements, and keep arrays for what they are naturally, the result of mapping over data.

What a fragment cannot do

Because a fragment renders no element, it has nowhere to put a class, a style, an event handler or a ref. <Fragment className="row"> does nothing useful, and the only prop React reads from a fragment is key. When you need to style the group or listen for clicks on it, you need a real element after all, and that is fine: pick one that is valid where it sits, like a <tbody> to group rows or a <li> to hold a list item.

A fragment works anywhere an element does, including inside conditional rendering: {show && <>...</>} shows or hides the whole group at once.

Frequently Asked Questions

What is a Fragment in React?

A Fragment is a wrapper that lets a component return several elements as one value without adding a DOM node. <><h1 /><p /></> puts the h1 and the p directly into the parent.

What is the difference between <> and <Fragment>?

They produce the same thing. <>...</> is the short syntax. <Fragment> is the long form you import from react, and it is the only one that can take a key, which you need when you render fragments in a list.

Why use a Fragment instead of a div?

A div is a real element. It can break layouts that expect direct children (flex and grid containers, ul, table, tr) and it adds noise to the DOM. A Fragment adds nothing.

Can a Fragment have a className or onClick?

No. A Fragment renders no element, so there is nothing to put a class, a style or an event handler on. The only prop it accepts is key. If you need any of those, use a real element.

Why do I get "Adjacent JSX elements must be wrapped in an enclosing tag"?

A component returned two sibling elements with nothing around them. JSX compiles to a single expression, so wrap the siblings in <>...</>.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED