Fragment in React JS
What is a React Fragment?
A React Fragment is a lightweight component that allows you to group multiple elements without adding an extra node to the DOM. Normally, when you return multiple elements from a React component, they must be wrapped in a single parent element, such as a <div>. However, this can lead to unnecessary DOM nodes, which may clutter the structure and affect performance.
Fragments solve this problem by acting as an invisible wrapper. They group elements together without rendering an additional DOM element.
Why Use Fragments?
Avoid Unnecessary DOM Nodes: Adding extra
<div>or other wrapper elements can bloat the DOM, making it harder to manage and potentially impacting performance. Fragments help keep the DOM clean and minimal.Improved Performance: By avoiding unnecessary nodes, Fragments can improve rendering performance, especially in large applications with complex component trees.
Better Semantics: Using Fragments allows you to maintain semantic HTML. For example, you can group table rows (
<tr>) or list items (<li>) without introducing invalid or unnecessary parent elements.
How to Use React Fragments
React provides two ways to use Fragments: the <React.Fragment> component and its shorthand syntax.
1. Using <React.Fragment>
The most explicit way to use a Fragment is by wrapping your elements in <React.Fragment> tags. Here’s an example:
import React from 'react';
function App() {
return (
<React.Fragment>
<h1>Hello, World!</h1>
<p>Welcome to React Fragments.</p>
</React.Fragment>
);
}
export default App;
In this example, the <h1> and <p> elements are grouped together without adding an extra wrapper element to the DOM.
2. Using the Shorthand Syntax (<> and </>)
React also provides a shorthand syntax for Fragments, which is more concise and easier to read. Here’s the same example using the shorthand:
import React from 'react';
function App() {
return (
<>
<h1>Hello, World!</h1>
<p>Welcome to React Fragments.</p>
</>
);
}
export default App;
The <> and </> tags work exactly like <React.Fragment> but are shorter and cleaner. However, note that the shorthand syntax does not support attributes.
Adding Keys to Fragments
In some cases, you may need to add a key attribute to a Fragment, such as when rendering a list of elements. This is only possible with the full <React.Fragment> syntax, as the shorthand does not support attributes.
Example:
import React from' react';
function ItemList({ items }) {
return (
<React.Fragment>
{items.map((item, index) => (
<React.Fragment key={index}>
<h2>{item.title}</h2>
<p>{item.description}</p>
</React.Fragment>
))
}
</React.Fragment>
);
}
export default ItemList
In this example, each React.Fragment in the map function is assigned a unique key to help React efficiently update the DOM.
When to Use Fragments
Fragments are particularly useful in the following scenarios:
- Returning Multiple Elements: When a component needs to return multiple sibling elements without wrapping them in an extra DOM node.
- Rendering Lists: When rendering lists of elements, especially when keys are required.
- Table Rows and Columns: When working with
<table>,<thead>,<tbody>,<tr>, and<td>elements, where adding extra wrappers can break the table structure.
For example, in a table:
import React from 'react';
function Table() {
return (
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<>
<tr>
<td>John</td>
<td>30</td>
</tr>
<tr>
<td>Jane</td>
<td>25</td>
</tr>
</>
</tbody>
</table>
);
}
export default Table;
Here, the Fragment ensures that the table structure remains valid while grouping the rows.
