Skip to content

Commit 276f55e

Browse files
docs: translated Fragments.md (#94)
docs: translated Fragments.md
2 parents 418d704 + 25c9b92 commit 276f55e

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

content/docs/fragments.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Fragments
44
permalink: docs/fragments.html
55
---
66

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 其中一種常見的使用情況是在一個 component 中回傳多個 element,fragment 讓你能夠在不用增加額外 DOM 節點的情況下,重新組合 child component。
88

99
```js
1010
render() {
@@ -18,11 +18,12 @@ render() {
1818
}
1919
```
2020

21-
There is also a new [short syntax](#short-syntax) for declaring them.
21+
還有一種[簡寫語法](#short-syntax)可以用來宣告 fragment。
22+
<!-- There is also a new [short syntax](#short-syntax) for declaring them. -->
2223

23-
## Motivation {#motivation}
24+
## 動機 {#motivation}
2425

25-
A common pattern is for a component to return a list of children. Take this example React snippet:
26+
常見的情況是在 component 裡回傳一連串的 child element,看看這個 React 的程式碼片段:
2627

2728
```jsx
2829
class Table extends React.Component {
@@ -38,7 +39,7 @@ class Table extends React.Component {
3839
}
3940
```
4041

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.
42+
為了使 render 出來的 HTML 是有效的,`<Columns />` 需要回傳多個 `<td>` element。如果將 parent div 元素放在 `<Columns />` 中的 `render()` 區塊,將會使生成的 HTML 無效。
4243

4344
```jsx
4445
class Columns extends React.Component {
@@ -53,7 +54,7 @@ class Columns extends React.Component {
5354
}
5455
```
5556

56-
results in a `<Table />` output of:
57+
`<Table />` 內的輸出如下:
5758

5859
```jsx
5960
<table>
@@ -66,9 +67,9 @@ results in a `<Table />` output of:
6667
</table>
6768
```
6869

69-
Fragments solve this problem.
70+
這個問題交給 fragment 解決。
7071

71-
## Usage {#usage}
72+
## 使用方式 {#usage}
7273

7374
```jsx{4,7}
7475
class Columns extends React.Component {
@@ -83,7 +84,7 @@ class Columns extends React.Component {
8384
}
8485
```
8586

86-
which results in a correct `<Table />` output of:
87+
會讓 `<Table />` 得到一個正確的輸出:
8788

8889
```jsx
8990
<table>
@@ -94,9 +95,9 @@ which results in a correct `<Table />` output of:
9495
</table>
9596
```
9697

97-
### Short Syntax {#short-syntax}
98+
### 簡寫語法 {#short-syntax}
9899

99-
There is a new, shorter syntax you can use for declaring fragments. It looks like empty tags:
100+
你可以用新的簡寫語法來宣告 fragment,它看起來就像空標籤:
100101

101102
```jsx{4,7}
102103
class Columns extends React.Component {
@@ -110,19 +111,18 @@ class Columns extends React.Component {
110111
}
111112
}
112113
```
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 和 attribute。
115115

116116
### Keyed Fragments {#keyed-fragments}
117117

118-
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:
118+
透過明確宣告 `<React.Fragment>` 的 fragment 可能會遇到帶有 key 的情況。一個使用案例是將它 mapping 到 fragment array。舉例來說,像下方程式碼一樣建立一個敘述列表:
119119

120120
```jsx
121121
function Glossary(props) {
122122
return (
123123
<dl>
124124
{props.items.map(item => (
125-
// Without the `key`, React will fire a key warning
125+
// 如果缺少 keyReact 會發出一個缺少 key 的警告
126126
<React.Fragment key={item.id}>
127127
<dt>{item.term}</dt>
128128
<dd>{item.description}</dd>
@@ -133,8 +133,8 @@ function Glossary(props) {
133133
}
134134
```
135135

136-
`key` is the only attribute that can be passed to `Fragment`. In the future, we may add support for additional attributes, such as event handlers.
136+
目前 `key` 是唯一可以傳遞給 `Fragment` 的 attribute。之後我們可能會支援更多的 attribute,像是 event handler。
137137

138138
### Live Demo {#live-demo}
139139

140-
You can try out the new JSX fragment syntax with this [CodePen](https://codepen.io/reactjs/pen/VrEbjE?editors=1000).
140+
你可以透過 [CodePen](https://codepen.io/reactjs/pen/VrEbjE?editors=1000) 嘗試新的 JSX fragment 語法。

0 commit comments

Comments
 (0)