You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Wrap elements in `<Fragment>`to group them together in situations where you need a single element. Grouping elements in `Fragment` has no effect on the resulting DOM; it is the same as if the elements were not grouped. The empty JSX tag `<></>`is shorthand for`<Fragment></Fragment>` in most cases.
26
+
当你需要单个元素时,你可以使用 `<Fragment>`将其他元素组合起来,使用 `<Fragment>` 组合后的元素不会对 DOM 产生影响,就像元素没有被组合一样。在大多数情况下,`<Fragment></Fragment>`可以简写为空的 JSX 元素`<></>`。
27
27
28
-
#### Props {/*props*/}
28
+
#### 参数 {/*props*/}
29
29
30
-
-**optional**`key`: Fragments declared with the explicit `<Fragment>`syntax may have [keys.](/learn/rendering-lists#keeping-list-items-in-order-with-key)
-If you want to pass `key`to a Fragment, you can't use the `<>...</>` syntax. You have to explicitly import `Fragment` from `'react'`and render `<Fragment key={yourKey}>...</Fragment>`.
-React does not [reset state](/learn/preserving-and-resetting-state) when you go from rendering `<><Child /></>`to `[<Child />]`or back, or when you go from rendering `<><Child /></>`to`<Child />`and back. This only works a single level deep: for example, going from `<><><Child /></></>`to`<Child />`resets the state. See the precise semantics [here.](https://gist.github.com/clemmy/b3ef00f9507909429d8aa0d3ee4f986b)
### Returning multiple elements {/*returning-multiple-elements*/}
42
+
### 返回多个元素 {/*returning-multiple-elements*/}
43
43
44
-
Use`Fragment`, or the equivalent `<>...</>`syntax, to group multiple elements together. You can use it to put multiple elements in any place where a single element can go. For example, a component can only return one element, but by using a Fragment you can group multiple elements together and then return them as a group:
Fragments are useful because grouping elements with a Fragment has no effect on layout or styles, unlike if you wrapped the elements in another container like a DOM element. If you inspect this example with the browser tools, you'll see that all `<h1>`and`<article>` DOM nodes appear as siblings without wrappers around them:
57
+
`Fragment` 作用很大,它与将元素包裹在一个 DOM 容器中不同,使用 `Fragment` 对元素进行组合后不会影响布局和样式。如果你使用浏览器调试工具查看这个示例,你会发现所有的 `<h1>`和`<article>` DOM 节点都是没有父元素的兄弟节点:
58
58
59
59
<Sandpack>
60
60
@@ -94,9 +94,9 @@ function PostBody({ body }) {
94
94
95
95
<DeepDive>
96
96
97
-
#### How to write a Fragment without the special syntax? {/*how-to-write-a-fragment-without-the-special-syntax*/}
Here's a situation where you need to write `Fragment` explicitly instead of using the `<></>` syntax. When you [render multiple elements in a loop](/learn/rendering-lists), you need to assign a `key` to each element. If the elements within the loop are Fragments, you need to use the normal JSX element syntax in order to provide the `key`attribute:
0 commit comments