Skip to content

片段

一句话解释

片段是 特殊 React 元素那个分组多个 children 不使用添加 extra wrapper DOM 节点.

用大白话说

React 组件必须返回单个根 元素. 不使用片段, 你'd 有到 wrap 多个元素 in <div>, which adds an unwanted extra node to the DOM — breaking CSS layouts, adding nesting, and confusing semantic HTML.

Fragments solve this. Write <>...</> (shorthand) or <React.片段>...</React.片段> to group children without a wrapper. The Fragment renders nothing to the DOM — its children appear as if they were returned directly.

The explicit <React.片段> form also accepts a 键 prop, which is useful when mapping a list where each item returns multiple elements.

示例

jsx
function Item() {
  return (
    <>
      <dt>Term</dt>
      <dd>Definition</dd>
    </>
  );
}

// No extra <div> in the DOM — just <dt> and <dd> directly.

片段分组 <dt> and <dd> 不使用添加 wrapper div, preserving 正确 HTML 结构 for 定义列表.

相关术语

相关资源

相关课程

  • 片段

    学习片段 for cleaner markup.

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表