React Fragments
Learn how React Fragments let you group multiple elements without adding an unnecessary DOM element.
1. What Are React Fragments?
A React component can return multiple sibling elements by wrapping them in a Fragment. A Fragment groups elements without introducing an extra DOM node.
div.
function App() {
return (
<React.Fragment>
<h2>Welcome to CIIT Training Institute</h2>
<p>Learn React step by step.</p>
</React.Fragment>
);
}
export default App;
2. React Fragment Diagram
Compare a normal wrapper element with a Fragment. The Fragment groups its children without creating another DOM element.
Without Fragment
The wrapper creates an additional DOM element.
With Fragment
Only the child elements appear in the DOM.
3. Using React.Fragment
The full React.Fragment syntax groups
sibling elements. In this example, React is imported
so the React namespace is available.
import React from "react";
function App() {
return (
<React.Fragment>
<h2>Our Courses</h2>
<p>Java Full Stack</p>
<p>.NET Full Stack</p>
</React.Fragment>
);
}
export default App;
Expected Browser Output
Our Courses
Java Full Stack
.NET Full Stack
4. Short Fragment Syntax
React also supports the shorter syntax
<> ... </>. It is a convenient
way to group sibling elements.
function App() {
return (
<>
<h2>Student Details</h2>
<p>Name: Priya</p>
<p>Course: React JS</p>
</>
);
}
export default App;
key attribute.
When rendering Fragment elements in a list and a key is
needed, use the full React.Fragment syntax.
5. Fragments in Lists
When one list item needs to produce multiple sibling elements, a keyed Fragment can group them without adding a wrapper to the DOM.
import React from "react";
function App() {
const students = [
{ id: 1, name: "Priya", course: "React JS" },
{ id: 2, name: "Rahul", course: ".NET Full Stack" },
{ id: 3, name: "Amit", course: "Python" }
];
return (
<dl>
{students.map((student) => (
<React.Fragment key={student.id}>
<dt>{student.name}</dt>
<dd>{student.course}</dd>
</React.Fragment>
))}
</dl>
);
}
export default App;
Expected Browser Output
- Priya
- React JS
- Rahul
- .NET Full Stack
- Amit
- Python
6. React Fragment vs div
| Feature | React Fragment | div Element |
|---|---|---|
| Creates a DOM element | No | Yes |
| Groups sibling elements | Yes | Yes |
| Can have CSS classes | No | Yes |
| Can have a key | Full syntax only | Yes |
| Useful for styling a wrapper | No | Yes |
div when a real wrapper is needed for
layout, styling, or attributes. Use a Fragment when you
only need to group elements.
7. Common Mistakes
1. Incorrect Fragment closing syntax
Incorrect:
<>Hello</React.Fragment>
Correct:
<>Hello</>
2. Adding a className to a Fragment
Fragments do not support regular DOM attributes such
as className. Use a real element if you
need styling on a wrapper.
3. Using short syntax when a key is required
Use React.Fragment with a stable
key when grouping multiple elements
inside a mapped list.
8. Practice Questions
- Return a heading and two paragraphs using short Fragment syntax.
- Rewrite a component that uses a wrapper div by using a Fragment.
- Create a student list using keyed Fragments.
- Explain when a div is more appropriate than a Fragment.
- Identify why a Fragment cannot receive a className.
Summary
React Fragments allow you to group multiple JSX elements without adding an extra DOM node. You can use the short syntax <>...</> for simple grouping or React.Fragment when you need to provide a key. Fragments help keep your component structure clean while rendering headings, paragraphs, and other sibling elements.