Skip to content

Commit 88bd35e

Browse files
NewVo1dawxiaoxian2020XleineYucohny
authored
docs(cn): translate reference/react/Fragment into Chinese (#1174)
Co-authored-by: Xavi Lee <awxiaoxian2020@163.com> Co-authored-by: Xleine <919143384@qq.com> Co-authored-by: Yucohny <79147654+Yucohny@users.noreply.github.com>
1 parent 0764f5f commit 88bd35e

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/content/reference/react/Fragment.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: <Fragment> (<>...</>)
44

55
<Intro>
66

7-
`<Fragment>`, often used via `<>...</>` syntax, lets you group elements without a wrapper node.
7+
`<Fragment>` 通常使用 `<>...</>` 代替,它们都允许你在不添加额外节点的情况下将子元素组合。
88

99
```js
1010
<>
@@ -19,29 +19,29 @@ title: <Fragment> (<>...</>)
1919

2020
---
2121

22-
## Reference {/*reference*/}
22+
## 参考 {/*reference*/}
2323

2424
### `<Fragment>` {/*fragment*/}
2525

26-
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 元素 `<></>`
2727

28-
#### Props {/*props*/}
28+
#### 参数 {/*props*/}
2929

30-
- **optional** `key`: Fragments declared with the explicit `<Fragment>` syntax may have [keys.](/learn/rendering-lists#keeping-list-items-in-order-with-key)
30+
- **可选** `key`:列表中 `<Fragment>` 的可以拥有 [keys](/learn/rendering-lists#keeping-list-items-in-order-with-key)
3131

32-
#### Caveats {/*caveats*/}
32+
#### 注意事项 {/*caveats*/}
3333

34-
- 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>`.
34+
- 如果你要传递 `key` 给一个 `<Fragment>`,你不能使用 `<>...</>`,你必须从 `'react'` 中导入 `Fragment` 且表示为`<Fragment key={yourKey}>...</Fragment>`
3535

36-
- 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)
36+
- 当你要从 `<><Child /></>` 转换为 `[<Child />]` `<><Child /></>` 转换为 `<Child />`,React 并不会[重置 state](/learn/preserving-and-resetting-state)。这个规则只在一层深度的情况下生效,如果从 `<><><Child /></></>` 转换为 `<Child />` 则会重置 state。在[这里](https://gist.github.com/clemmy/b3ef00f9507909429d8aa0d3ee4f986b)查看更详细的介绍。
3737

3838
---
3939

40-
## Usage {/*usage*/}
40+
## 用法 {/*usage*/}
4141

42-
### Returning multiple elements {/*returning-multiple-elements*/}
42+
### 返回多个元素 {/*returning-multiple-elements*/}
4343

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:
44+
使用 `Fragment` 或简写语法 `<>...</>` 将多个元素组合在一起,你可以使用它将多个元素等效于单个元素。例如,一个组件只能返回一个元素,但是可以使用 `Fragment` 将多个元素组合在一起,并作为一个元素返回:
4545

4646
```js {3,6}
4747
function Post() {
@@ -54,7 +54,7 @@ function Post() {
5454
}
5555
```
5656

57-
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 节点都是没有父元素的兄弟节点:
5858

5959
<Sandpack>
6060

@@ -94,9 +94,9 @@ function PostBody({ body }) {
9494

9595
<DeepDive>
9696

97-
#### How to write a Fragment without the special syntax? {/*how-to-write-a-fragment-without-the-special-syntax*/}
97+
#### 如何使用完整的语法编写 `Fragment` {/*how-to-write-a-fragment-without-the-special-syntax*/}
9898

99-
The example above is equivalent to importing `Fragment` from React:
99+
这个示例从 React 中导入了 `Fragment`
100100

101101
```js {1,5,8}
102102
import { Fragment } from 'react';
@@ -111,15 +111,15 @@ function Post() {
111111
}
112112
```
113113

114-
Usually you won't need this unless you need to [pass a `key` to your `Fragment`.](#rendering-a-list-of-fragments)
114+
通常你不需要这样,除非你需要将 [`key` 传递给 `Fragment`](#rendering-a-list-of-fragments)
115115

116116
</DeepDive>
117117

118118
---
119119

120-
### Assigning multiple elements to a variable {/*assigning-multiple-elements-to-a-variable*/}
120+
### 分配多个元素给一个变量 {/*assigning-multiple-elements-to-a-variable*/}
121121

122-
Like any other element, you can assign Fragment elements to variables, pass them as props, and so on:
122+
和其他元素一样,你可以将 `Fragment` 元素分配给变量,作为 props 传递等:
123123

124124
```js
125125
function CloseDialog() {
@@ -139,9 +139,9 @@ function CloseDialog() {
139139
140140
---
141141
142-
### Grouping elements with text {/*grouping-elements-with-text*/}
142+
### 组合文本与组件 {/*grouping-elements-with-text*/}
143143
144-
You can use `Fragment` to group text together with components:
144+
你可以使用 `Fragment` 将文本与组件组合在一起:
145145
146146
```js
147147
function DateRangePicker({ start, end }) {
@@ -158,9 +158,9 @@ function DateRangePicker({ start, end }) {
158158
159159
---
160160
161-
### Rendering a list of Fragments {/*rendering-a-list-of-fragments*/}
161+
### 渲染 `Fragment` 列表 {/*rendering-a-list-of-fragments*/}
162162
163-
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:
163+
在这种情况下,你需要显式地表示为 `Fragment`,而不是使用简写语法 `<></>`。当你在[循环中渲染多个元素](/learn/rendering-lists)时,你需要为每一个元素分配一个 `key`。如果这个元素为 `Fragment` 时,则需要使用普通的 JSX 语法来提供 `key` 属性。
164164
165165
```js {3,6}
166166
function Blog() {
@@ -173,7 +173,7 @@ function Blog() {
173173
}
174174
```
175175
176-
You can inspect the DOM to verify that there are no wrapper elements around the Fragment children:
176+
你可以查看 DOM 以验证组合后的子元素实际上并没有父元素 `Fragment`
177177
178178
<Sandpack>
179179

0 commit comments

Comments
 (0)