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
Copy file name to clipboardexpand all lines: content/docs/fragments.md
+19-19
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: Fragments
4
4
permalink: docs/fragments.html
5
5
---
6
6
7
-
A common pattern in React is for a component to return multiple elements. Fragments let you group a list of children without adding extra nodes to the DOM.
7
+
React 中的一个常见模式是一个组件返回多个元素。Fragments 允许你将子列表分组,而无需向 DOM 添加额外节点。
8
8
9
9
```js
10
10
render() {
@@ -18,11 +18,11 @@ render() {
18
18
}
19
19
```
20
20
21
-
There is also a new [short syntax](#short-syntax) for declaring them, but it isn't supported by all popular tools yet.
21
+
还有一种新的[短语法](#short-syntax)可用于声明它们,但尚未得到所有流行工具的支持。
22
22
23
-
## Motivation {#motivation}
23
+
## 动机 {#motivation}
24
24
25
-
A common pattern is for a component to return a list of children. Take this example React snippet:
25
+
一种常见模式是组件返回一个子元素列表。以此 React 代码片段为例:
26
26
27
27
```jsx
28
28
classTableextendsReact.Component {
@@ -38,7 +38,7 @@ class Table extends React.Component {
38
38
}
39
39
```
40
40
41
-
`<Columns />`would need to return multiple `<td>`elements in order for the rendered HTML to be valid. If a parent div was used inside the `render()` of `<Columns />`, then the resulting HTML will be invalid.
41
+
`<Columns />`需要返回多个 `<td>`元素以使渲染的 HTML 有效。如果在 `<Columns />` 的 `render()` 中使用了父 div,则生成的 HTML 将无效。
42
42
43
43
```jsx
44
44
classColumnsextendsReact.Component {
@@ -53,7 +53,7 @@ class Columns extends React.Component {
53
53
}
54
54
```
55
55
56
-
results in a `<Table />`output of:
56
+
得到一个 `<Table />`输出:
57
57
58
58
```jsx
59
59
<table>
@@ -66,9 +66,9 @@ results in a `<Table />` output of:
66
66
</table>
67
67
```
68
68
69
-
Fragments solve this problem.
69
+
Fragments 解决了这个问题。
70
70
71
-
## Usage {#usage}
71
+
## 用法 {#usage}
72
72
73
73
```jsx{4,7}
74
74
class Columns extends React.Component {
@@ -83,7 +83,7 @@ class Columns extends React.Component {
83
83
}
84
84
```
85
85
86
-
which results in a correct `<Table />` output of:
86
+
这样可以正确的输出 `<Table />`:
87
87
88
88
```jsx
89
89
<table>
@@ -94,9 +94,9 @@ which results in a correct `<Table />` output of:
94
94
</table>
95
95
```
96
96
97
-
### Short Syntax {#short-syntax}
97
+
### 短语法 {#short-syntax}
98
98
99
-
There is a new, shorter syntax you can use for declaring fragments. It looks like empty tags:
99
+
你可以使用一种新的,且更简短的语法来声明 Fragments。它看起来像空标签:
100
100
101
101
```jsx{4,7}
102
102
class Columns extends React.Component {
@@ -111,20 +111,20 @@ class Columns extends React.Component {
111
111
}
112
112
```
113
113
114
-
You can use `<></>` the same way you'd use any other element except that it doesn't support keys or attributes.
114
+
你可以像使用任何其他元素一样使用 `<></>`,除了它不支持 key 或属性。
115
115
116
-
Note that **[many tools don't support it yet](/blog/2017/11/28/react-v16.2.0-fragment-support.html#support-for-fragment-syntax)**so you might want to explicitly write `<React.Fragment>` until the tooling catches up.
Fragments declared with the explicit `<React.Fragment>`syntax may have keys. A use case for this is mapping a collection to an array of fragments -- for example, to create a description list:
0 commit comments