Skip to content

Commit

Permalink
docs(cn): translated content/docs/fragments.md into Chinese (reactjs#132
Browse files Browse the repository at this point in the history
)

translated fragments document
  • Loading branch information
chenshuai2144 authored and QC-L committed Mar 27, 2019
1 parent b160b3c commit a5e0297
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions content/docs/fragments.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Fragments
permalink: docs/fragments.html
---

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.
React 中的一个常见模式是一个组件返回多个元素。Fragments 允许你将子列表分组,而无需向 DOM 添加额外节点。

```js
render() {
Expand All @@ -18,11 +18,11 @@ render() {
}
```

There is also a new [short syntax](#short-syntax) for declaring them, but it isn't supported by all popular tools yet.
还有一种新的[短语法](#short-syntax)可用于声明它们,但尚未得到所有流行工具的支持。

## Motivation {#motivation}
## 动机 {#motivation}

A common pattern is for a component to return a list of children. Take this example React snippet:
一种常见模式是组件返回一个子元素列表。以此 React 代码片段为例:

```jsx
class Table extends React.Component {
Expand All @@ -38,7 +38,7 @@ class Table extends React.Component {
}
```

`<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.
`<Columns />` 需要返回多个 `<td>` 元素以使渲染的 HTML 有效。如果在 `<Columns />``render()` 中使用了父 div,则生成的 HTML 将无效。

```jsx
class Columns extends React.Component {
Expand All @@ -53,7 +53,7 @@ class Columns extends React.Component {
}
```

results in a `<Table />` output of:
得到一个 `<Table />` 输出:

```jsx
<table>
Expand All @@ -66,9 +66,9 @@ results in a `<Table />` output of:
</table>
```

Fragments solve this problem.
Fragments 解决了这个问题。

## Usage {#usage}
## 用法 {#usage}

```jsx{4,7}
class Columns extends React.Component {
Expand All @@ -83,7 +83,7 @@ class Columns extends React.Component {
}
```

which results in a correct `<Table />` output of:
这样可以正确的输出 `<Table />`

```jsx
<table>
Expand All @@ -94,9 +94,9 @@ which results in a correct `<Table />` output of:
</table>
```

### Short Syntax {#short-syntax}
### 短语法 {#short-syntax}

There is a new, shorter syntax you can use for declaring fragments. It looks like empty tags:
你可以使用一种新的,且更简短的语法来声明 Fragments。它看起来像空标签:

```jsx{4,7}
class Columns extends React.Component {
Expand All @@ -111,20 +111,20 @@ class Columns extends React.Component {
}
```

You can use `<></>` the same way you'd use any other element except that it doesn't support keys or attributes.
你可以像使用任何其他元素一样使用 `<> </>`,除了它不支持 key 或属性。

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.
请注意 **[目前许多工具尚不支持](/blog/2017/11/28/react-v16.2.0-fragment-support.html#support-for-fragment-syntax)** ,所以直到工具支持之前,你可能要显式的使用 `<React.Fragment>`

### Keyed Fragments {#keyed-fragments}
### 带 key 的 Fragments {#keyed-fragments}

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:
使用显式 `<React.Fragment>` 语法声明的片段可能具有 key。一个使用场景是将一个集合映射到一个 Fragments 数组 - 举个例子,创建一个描述列表:

```jsx
function Glossary(props) {
return (
<dl>
{props.items.map(item => (
// Without the `key`, React will fire a key warning
// 没有`key`React 会发出一个关键警告
<React.Fragment key={item.id}>
<dt>{item.term}</dt>
<dd>{item.description}</dd>
Expand All @@ -135,8 +135,8 @@ function Glossary(props) {
}
```

`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.
`key` 是唯一可以传递给 `Fragment` 的属性。未来我们可能会添加对其他属性的支持,例如事件。

### Live Demo {#live-demo}
### 在线 Demo {#live-demo}

You can try out the new JSX fragment syntax with this [CodePen](https://codepen.io/reactjs/pen/VrEbjE?editors=1000).
你可以在 [CodePen](https://codepen.io/reactjs/pen/VrEbjE?editors=1000) 中尝试这个新的 JSX Fragment 语法。

0 comments on commit a5e0297

Please sign in to comment.