Fragment (React)

A Fragment lets a component return multiple elements without wrapping them in an extra DOM node like a div. You write it as <React.Fragment> or, more commonly, the shorthand empty tags <> </>.

React

JSX requires a component to return a single parent element. Sometimes you don't want an extra <div> cluttering the page just to satisfy that rule — Fragments solve this by grouping children without adding anything to the actual HTML output.

The shorthand <>...</> is the everyday way to use a Fragment. If you need to give the Fragment a key (for example in a list), use the full <React.Fragment key={...}> form, since the shorthand can't take attributes.

Examples

JSX — Renders the heading and paragraph side by side with no extra wrapper element added to the page's HTML.

function Info() {
  return (
    <>
      <h2>Title</h2>
      <p>Description</p>
    </>
  );
}

Learn it in a lesson

See also

Back to the Dictionary