片段
分组元素不使用添加 extra DOM 节点 using React 片段.
你将学到
- 为什么片段 exist and problem 它们解决
- Using shorthand <></> 语法
- Using React.片段使用键 prop
- 当片段 matter for CSS and 语义
概念讲解
The Extra DOM Node Problem
React components must return a single root element. Before fragments, the common workaround was to wrap sibling elements in a <div>. This added unnecessary DOM nodes that could break CSS layouts (like CSS Grid or Flexbox, which depend on direct children), invalidate HTML semantics (like <li> inside <ul>, which cannot have a wrapping <div>), and slow down rendering with extra DOM nodes. Fragments solve this by letting you group elements without adding a DOM node.
The Shorthand Syntax
The most common fragment syntax is the shorthand: <></>. It is empty tags that wrap multiple elements without rendering anything to the DOM. It is concise and readable. However, the shorthand does not support attributes — most importantly, it cannot take a 键 prop. When you need a key (in a list), you must use the explicit <React.片段> form.
React.Fragment with key
When rendering a list of fragments, each fragment needs a 键 prop just like any other list item. The shorthand <></> cannot accept a key, so you must use <React.片段 键={项.id}>. This is common when a list item needs to return multiple sibling elements, like a table row with multiple cells or a definition list with terms and descriptions.
When Fragments Matter
Fragments matter most when the DOM structure is important: CSS Grid and Flexbox layouts that rely on direct children, HTML tables (<tr> cannot contain a <div>), definition lists, and any case where an extra <div> would break the HTML structure or CSS selectors. They also matter for performance — every DOM node has a cost, and eliminating unnecessary wrappers can measurably speed up large lists.
Fragments vs Arrays
Before fragments existed, developers returned arrays of elements from components. This still works but requires explicit keys on each element and is less readable. Fragments are the modern, preferred approach — they do not require keys (unless in a list) and are more expressive. Always prefer fragments over array returns for grouping sibling elements.
示例代码
import React, { Fragment } from "react";
// Shorthand fragment — no extra DOM node.
function Header() {
return (
<>
<h1>Page Title</h1>
<p>Subtitle text</p>
</>
);
}
// Explicit Fragment with key — needed in lists.
function DefinitionList({ terms }) {
return (
<dl>
{terms.map(term => (
<Fragment key={term.id}>
<dt>{term.name}</dt>
<dd>{term.description}</dd>
</Fragment>
))}
</dl>
);
}
// Without fragments, this would need a wrapping <div> that breaks <dl> semantics.
function App() {
const terms = [
{ id: 1, name: "React", description: "A UI library" },
{ id: 2, name: "Fragment", description: "A node-less wrapper" },
];
return (
<>
<Header />
<DefinitionList terms={terms} />
</>
);
}
shorthand <> 分组请求头's 元素. explicit 片段使用键 lets DefinitionList 返回 dt/dd pairs 不使用 wrapper div, preserving dl 语义.
动手试一试
- HTML Entity Encoder
理解 HTML 结构规则那个片段 help preserve.
- Code Minifier
See 如何片段编译到 React.片段 calls.
常见错误
错误写法
Using shorthand <></> 当你 需要键 prop
正确做法
shorthand 语法 cannot 接受任何 attributes. 当你 需要键 (in 列表), use explicit <React.片段键={id}> 表单相反的 <></>.
错误写法
Wrapping 列表项 in 不必要 <div> 元素
正确做法
Use 片段相反的 divs 当分组 siblings. 这个 avoids extra DOM 节点那个能 break CSS 布局 and HTML 语义, 特别是 in 表 and 列表.
练习 片段
6 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。