Skip to content
Tutorials Center
My learning

Topics

React Reference ยท Components and Special Elements

The Fragment component

Reference for React Fragment: return several siblings without an extra DOM node, use the <></> shorthand, and know when you need the long form with a key.

Fragment lets a component return several elements as siblings without adding a wrapper element to the DOM. A component must return one thing, but that one thing can be a fragment holding many children. React renders the children and leaves no trace of the fragment itself: no <div>, no attributes, no extra layout box.

You almost always write it as the short syntax <>...</>. The long form <Fragment>...</Fragment> does the same job and is needed only when you want to pass a key. Fragments exist because some HTML structures are strict about their children: a <tr> may only contain <td> and <th>, a <ul> only <li>, and a stray wrapper <div> breaks tables, lists, flexbox and grid layouts.

JSX
import { Fragment } from 'react';

function Pair() {
  return (
    <>
      <h2>Title</h2>
      <p>Paragraph</p>
    </>
  );
}

Try it

Table cells without a wrapper
Fragments leave no node behind

Syntax

JSX
<Fragment key={optionalKey}>{children}</Fragment>
<>{children}</>
Name Description
children Any renderable content: elements, strings, numbers, arrays, other fragments. They are rendered directly into the parent DOM node.
key Optional. Only the long <Fragment> form accepts it. Needed when a fragment is one item of a mapped list.

Returns: nothing of its own. A fragment has no DOM node and no instance.

Edge cases: the only prop a fragment accepts besides children is key. Passing className, style or an event handler does nothing useful; if you need those, you need a real element.

Usage notes

  • The short syntax cannot take a key. <key={id}> is not valid JSX. In a list, write <Fragment key={id}> and import Fragment. See Keyed fragments.
  • State is preserved by position. Wrapping children in a fragment, or unwrapping them, does not remount them when the surrounding structure is otherwise unchanged. <><A /><B /></> and <A /><B /> in the same spot keep the same state for A and B. (A fragment nested one level deeper does change the tree shape, which resets state.)
  • Nested fragments are allowed and flatten in the DOM, but a fragment inside a fragment inside the same slot is usually a sign you can simplify.
  • Rendering null is different. null, undefined and false render nothing, but they still occupy a position. An empty fragment <></> also renders nothing.
  • CSS and layout. Because no wrapper exists, a parent flex or grid container sees the fragment children as its direct children. That is often exactly the reason to use a fragment.
  • Whitespace. JSX trims whitespace between lines, so fragments never insert stray text nodes.
  • No DOM node of its own. In stable releases you cannot put a ref on a fragment to get a DOM element. If you need a handle on the group, wrap the children in a real element.
  • Add an element when it carries meaning. If the group needs a class, an ARIA role or a click handler, use a real <div>, <section> or <ul>. Do not use a fragment to avoid markup that you actually want.