React Interview Questions
React.js
FrontendWeb DevelopmentQuestion 25
What are React fragments and how do you use them?
Answer:
React Fragments are a feature in React that allows you to group a list of children without adding extra nodes to the DOM. They are particularly useful when a component needs to return multiple elements, but you don't want to add an extra wrapper element (like a <div>
) around them, which can interfere with your layout or styling.
Why Use React Fragments?
- Avoid Unnecessary Wrappers: Prevents adding unnecessary DOM elements that can clutter the DOM structure.
- Improve Performance: Reduces the number of DOM nodes, potentially improving performance.
- Simplify Markup: Makes the code cleaner and easier to read by not adding extra markup.
How to Use React Fragments
There are two main ways to use React Fragments: using the <Fragment>
component and the shorthand syntax.
1. Using <Fragment>
You can use the <Fragment>
component provided by React.
import React, { Fragment } from 'react';
function MyComponent() {
return (
<Fragment>
<h1>Title</h1>
<p>Paragraph</p>
</Fragment>
);
}
export default MyComponent;
2. Using Shorthand Syntax
React also provides a shorthand syntax using empty tags (<>
and </>
), which is more concise.
import React from 'react';
function MyComponent() {
return (
<>
<h1>Title</h1>
<p>Paragraph</p>
</>
);
}
export default MyComponent;
Use Cases for React Fragments
Grouping Elements
When you need to return multiple elements from a component without adding an extra wrapper element.
function ListItem({ item }) {
return (
<>
<dt>{item.term}</dt>
<dd>{item.description}</dd>
</>
);
}
function List() {
const items = [
{ term: 'HTML', description: 'A markup language' },
{ term: 'CSS', description: 'A style sheet language' },
{ term: 'JavaScript', description: 'A programming language' },
];
return (
<dl>
{items.map((item, index) => (
<ListItem key={index} item={item} />
))}
</dl>
);
}
export default List;
Rendering Table Rows
When rendering table rows, using fragments can help avoid adding unwanted <div>
or other elements inside the table structure.
function TableRow({ data }) {
return (
<>
<tr>
<td>{data.name}</td>
<td>{data.age}</td>
</tr>
</>
);
}
function Table() {
const rows = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 },
];
return (
<table>
<tbody>
{rows.map((row, index) => (
<TableRow key={index} data={row} />
))}
</tbody>
</table>
);
}
export default Table;
Using key
with Fragments
If you need to use a key
attribute (typically in lists), you must use the <Fragment>
syntax, as the shorthand syntax does not support attributes.
import React, { Fragment } from 'react';
function ItemList({ items }) {
return (
<ul>
{items.map((item, index) => (
<Fragment key={index}>
<li>{item.name}</li>
<li>{item.value}</li>
</Fragment>
))}
</ul>
);
}
export default ItemList;
Conclusion
React Fragments are a powerful feature for managing the rendering of multiple elements without adding unnecessary wrapper elements to the DOM. They help keep the DOM clean, improve performance, and make the code more readable. By using either the <Fragment>
component or the shorthand syntax (<>
and </>
), you can effectively group elements in your React components.