React function components must always return a single top-level JSX element. However, when a component needs to return multiple top-level elements, React Fragments provide a way to do so without introducing unnecessary extra HTML elements.
Think of Fragments as invisible containers that group multiple elements together without affecting the actual structure of the rendered HTML.
By default, React requires that a component returns a single top-level element. If a component tries to return multiple sibling elements, React throws an error.
// This WILL NOT work
function MyComponent() {
return (
<h1>Abraham Lincoln</h1>
<p>Abraham Lincoln was the 16th President of the United States.</p>
);
}
The JSX above contains two top-level elements (<h1> and <p>), which will result in a compilation error.
To return multiple top-level elements, you have two options:
<div>One common solution is to wrap all elements inside a single container element, such as a <div>.
// This WILL work
function MyComponent() {
return (
<div>
<h1>Abraham Lincoln</h1>
<p>Abraham Lincoln was the 16th President of the United States.</p>
</div>
);
}
While this works, it introduces an unnecessary <div> into the real DOM, which might not always be desirable.
Instead of adding extra HTML elements, React provides a special component called React.Fragment that allows returning multiple elements without modifying the HTML structure.
React.Fragmentimport React from 'react';
function MyComponent() {
return (
<React.Fragment>
<h1>Abraham Lincoln</h1>
<p>Abraham Lincoln was the 16th President of the United States.</p>
</React.Fragment>
);
}
The React.Fragment tag allows multiple top-level elements to be grouped together without adding extra elements to the DOM.
React provides a shorthand syntax for Fragments: empty angle brackets <> ... </>. This is functionally identical to React.Fragment but does not require an explicit import.
function MyComponent() {
return (
<>
<h1>Abraham Lincoln</h1>
<p>Abraham Lincoln was the 16th President of the United States.</p>
</>
);
}
The shorthand version is preferred in most cases since it is more concise and does not require an additional import statement.
React.FragmentThere is only one case where you must use React.Fragment instead of the shorthand syntax: when you need to pass a key prop.
key Propfunction ItemList({ items }) {
return (
<React.Fragment>
{items.map(item => (
<React.Fragment key={item.id}>
<h3>{item.name}</h3>
<p>{item.description}</p>
</React.Fragment>
))}
</React.Fragment>
);
}
Since the shorthand syntax does not support props, you must use React.Fragment when passing a key.
<div> vs. FragmentLet’s compare using a <div> versus using a Fragment.
<div><div>
<h1>Title</h1>
<p>Some text here.</p>
</div>
Here, an extra <div> is added to the DOM, which might interfere with styling or layout.
<h1>Title</h1>
<p>Some text here.</p>
Using a Fragment avoids unnecessary wrapper elements in the final HTML output.
In this tutorial, you learned:
<div> to wrap multiple elements, but it adds unnecessary markup.React.Fragment or <> ... </>) allow returning multiple elements without adding extra DOM nodes.React.Fragment when passing a key prop.Using Fragments is a best practice for keeping your markup clean and avoiding unnecessary <div> elements. As you continue building React applications, you will find them essential for structuring components efficiently.