React Fragment (`<>`) vs. `<div>`: Which Should You Use?
This post was translated from Chinese by AI. If anything reads oddly, the Chinese original is authoritative. 中文原文
When building components with Next.js/React, two common wrappers are React Fragment (<>...</>) and a regular <div>...</div>. Although they may look like different tags in the syntax, they differ fundamentally in their rendered output and layout behavior. This article covers how they work, their impact on styling, and practical use cases to help you quickly understand and choose the right one.
1. What Is React Fragment (<>...</>)?
- Virtual container: A Fragment exists only during compilation and produces no extra tags in the DOM.
- No extra overhead: It creates no new nodes, so it does not affect the parent layout and cannot be targeted by CSS.
- Most common use: Returning multiple sibling elements from a component without adding an unnecessary wrapper.
function Header() {
return (
<>
<Logo />
<Navbar />
</>
);
}
Rendered output:
<logo></logo>
<nav></nav>
2. Characteristics of <div>...</div>
- Real DOM element: It produces a
<div>node in the final HTML. - Default
display:block: It takes up its own line by default, affecting the surrounding layout. - Supports styles and events: If you need to set a container's width, height, margins, or background color, or attach a click handler, you need a real element.
function Card({ children }) {
return <div className="shadow-lg p-4 rounded-md">{children}</div>;
}
Rendered output:
<div class="shadow-lg p-4 rounded-md">
...children
</div>
3. Layout and Styling Differences
| Feature | Fragment | `` |
|---|---|---|
| Rendered nodes | Produces no nodes | Renders as a block-level element |
| Layout impact | None | Takes up its own line by default; can participate in Flex / Grid layouts |
| Supports styling | No | Yes |
| Supports event handlers | No | Yes |
| Typical use case | Satisfying the “must have a parent element” requirement without changing the layout | Providing a container for styling, positioning, or interaction |
4. When Should You Use Fragment?
- The outer wrapper only satisfies syntax requirements: The component needs to return multiple elements without adding redundant structure to the DOM.
- Avoid disrupting the parent layout: Extra nodes can interfere with arrangements in Flex, Grid, Table, and similar layouts.
- Improve performance: Although the impact is negligible, one fewer node means less parsing and rendering work.
Note: If you need to add a
keyto a list item, use the explicit<React.Fragment key={...}>syntax.
5. When Should You Use <div>?
- You need styling or positioning: Set width, height, margins, backgrounds, and so on.
- You need interaction: Attach event handlers for clicks, dragging, and so on.
- You need semantic markup: Consider more appropriate elements such as
<section>,<article>, or<header>if you need semantic tags.
6. Practical Example: Rendering a List
function List({ items }) {
return (
<ul>
{items.map(item => (
// No extra div needed; use Fragment to avoid affecting ul styles
<li key={item.id} className="border-b">
<>
<span>{item.name}</span>
<span className="text-sm text-gray-500">{item.time}</span>
</>
</li>
))}
</ul>
);
}
Wrapping the <span> elements in a <div> instead would introduce unnecessary nesting inside the li and disrupt the inline layout.
7. Summary
- Fragment = transparent wrapper: No nodes, styles, or events; used only for structure.
<div>= real container: Creates a node, has default layout behavior, and supports styles and events.- How to choose:
- No layout impact and no need for styles/events — use Fragment.
- Need styling/interaction or layout control — use a real element, such as
<div>or another semantic tag.
Use these two wrappers appropriately in everyday development to keep the DOM structure clean while ensuring that layout and interactions work as intended.
Comments 0