In React, a component's render function can only return a single root element. This requirement originates from how JSX compiles down to standard JavaScript function calls.
Consider this invalid component:
1// ❌ Compilation Error: Adjacent JSX elements must be wrapped in an enclosing tag
2exportdefaultfunctionNavigation(){
3return(
4<ahref="/home">Home</a>
5<ahref="/courses">Courses</a>
6);
7}
When transpiled, this attempts to return two separate function calls simultaneously (return _jsx('a', ...) _jsx('a', ...);), which is invalid JavaScript syntax. A function can only return a single value.
2. The Traditional Workaround: Wrapper Divs
Historically, developers solved this constraint by wrapping sibling elements in an unnecessary <div>:
1exportdefaultfunctionNavigation(){
2return(
3<div>
4<ahref="/home">Home</a>
5<ahref="/courses">Courses</a>
6</div>
7);
8}
The Problems with Wrapper Divs:
1
DOM Bloat: Adding extraneous wrapper <div>s inflates the browser's DOM tree depth and increases memory consumption.
2
Broken CSS Layouts: CSS Grid and Flexbox rely on direct parent-child relationships. Inserting arbitrary <div> containers disrupts grid columns, flex alignment, and child selectors (.parent > *).
3
Invalid HTML Semantics: In elements like <table> or <ul>, only specific child elements (<tr>, <td>, <li>) are semantically and syntactically valid. Wrapping table cells inside a <div> invalidates HTML specifications.
1Invalid HTML Table:
2<table>
3 <tbody>
4 <tr>
5 <div> <!-- ❌ Invalid: <div> cannot be an immediate child of <tr> -->
6 <td>Column 1</td>
7 <td>Column 2</td>
8 </div>
9 </tr>
10 </tbody>
11</table>
3. The Solution: React Fragments
A React Fragment allows you to group a list of children without adding extra nodes to the real browser DOM. When React renders the component to the screen, the Fragment container is completely invisible and leaves no trace in the real DOM tree.
Full Syntax (<React.Fragment>)
1import React from'react';
2
3exportdefaultfunctionTableColumns(){
4return(
5<React.Fragment>
6<td>Data Point A</td>
7<td>Data Point B</td>
8</React.Fragment>
9);
10}
Short Syntax (<> ... </>)
React provides a concise empty-tag shortcut for fragments:
1exportdefaultfunctionNavigation(){
2return(
3<>
4<ahref="/home">Home</a>
5<ahref="/courses">Courses</a>
6<ahref="/contact">Contact</a>
7</>
8);
9}
4. When Must You Use the Full <React.Fragment> Syntax?
There is one critical scenario where the short syntax <> is not sufficient: when you need to pass a key prop while mapping over a list of items.
The short syntax <> does not accept any attributes or props. If you are rendering an array of fragments in a loop, you must import and use <React.Fragment key={item.id}>:
1import React from'react';
2
3exportdefaultfunctionGlossary({ terms }){
4return(
5<dl>
6{terms.map((item)=>(
7// ✅ Valid: Full React.Fragment supports the key attribute