Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(cn): translate content/docs/components-and-props.md into Chinese #79

Merged
merged 7 commits into from Feb 27, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
98 changes: 49 additions & 49 deletions content/docs/components-and-props.md
@@ -1,6 +1,6 @@
---
id: components-and-props
title: Components and Props
title: 组件 & Props
hijiangtao marked this conversation as resolved.
Show resolved Hide resolved
permalink: docs/components-and-props.html
redirect_from:
- "docs/reusable-components.html"
Expand All @@ -16,23 +16,23 @@ prev: rendering-elements.html
next: state-and-lifecycle.html
---

Components let you split the UI into independent, reusable pieces, and think about each piece in isolation. This page provides an introduction to the idea of components. You can find a [detailed component API reference here](/docs/react-component.html).
组件允许你将 UI 拆分为独立且可复用的代码片段,这样你就只需专注于构建每个独立的代码片段。本指南旨在介绍有关组件的概念。你可以在此处找到[有关组件 API 详情的参考](/docs/react-component.html)
QC-L marked this conversation as resolved.
Show resolved Hide resolved

Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called "props") and return React elements describing what should appear on the screen.
从概念上讲,组件就像 JavaScript 函数。它可以接收任意的入参(称之为 “props”),并返回要用于描述页面中展示内容的 React 元素。
QC-L marked this conversation as resolved.
Show resolved Hide resolved

## Function and Class Components
## 函数式组件与 class 组件

The simplest way to define a component is to write a JavaScript function:
定义组件最简单的方式就是编写 JavaScript 函数:

```js
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
```

This function is a valid React component because it accepts a single "props" (which stands for properties) object argument with data and returns a React element. We call such components "function components" because they are literally JavaScript functions.
该函数是一个有效的 React 组件,因为它接受单一带有数据的 “props”(代表属性)对象与并返回一个 React 元素。这类组件被称为“函数组件”,因为它本质上就是 JavaScript 函数。

You can also use an [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) to define a component:
你同时还可以使用 [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) 来定义组件:

```js
class Welcome extends React.Component {
Expand All @@ -42,27 +42,27 @@ class Welcome extends React.Component {
}
```

The above two components are equivalent from React's point of view.
React 的角度来思考,上述两个组件是等效的。
QC-L marked this conversation as resolved.
Show resolved Hide resolved

Classes have some additional features that we will discuss in the [next sections](/docs/state-and-lifecycle.html). Until then, we will use function components for their conciseness.
我们将[在下一章节](/docs/state-and-lifecycle.html)中讨论关于 class 的额外特性。在那之前,我们将使用较为简洁的函数式组件。
QC-L marked this conversation as resolved.
Show resolved Hide resolved

## Rendering a Component
## 组件渲染

Previously, we only encountered React elements that represent DOM tags:
之前,我们遇到的React 元素都只是 DOM 标签:
QC-L marked this conversation as resolved.
Show resolved Hide resolved

```js
const element = <div />;
```

However, elements can also represent user-defined components:
不过,React 元素也可以是用户自定义的组件:

```js
const element = <Welcome name="Sara" />;
```

When React sees an element representing a user-defined component, it passes JSX attributes to this component as a single object. We call this object "props".
React 元素为用户自定义组件时,它会将 JSX 所接收的属性作为单个对象传递给组件,这个对象被称之为 “props”。
QC-L marked this conversation as resolved.
Show resolved Hide resolved

For example, this code renders "Hello, Sara" on the page:
例如,这段代码会在页面上的内容渲染为 “Hello, Sara”:
QC-L marked this conversation as resolved.
Show resolved Hide resolved

```js{1,5}
function Welcome(props) {
Expand All @@ -76,26 +76,26 @@ ReactDOM.render(
);
```

[](codepen://components-and-props/rendering-a-component)
[在 CodePen 上试试](codepen://components-and-props/rendering-a-component)

Let's recap what happens in this example:
让我们来回顾一下这个例子中发生了什么:

1. We call `ReactDOM.render()` with the `<Welcome name="Sara" />` element.
2. React calls the `Welcome` component with `{name: 'Sara'}` as the props.
3. Our `Welcome` component returns a `<h1>Hello, Sara</h1>` element as the result.
4. React DOM efficiently updates the DOM to match `<h1>Hello, Sara</h1>`.
1. 我们针对 `<Welcome name="Sara" />` 调用了 `ReactDOM.render()` 函数。
QC-L marked this conversation as resolved.
Show resolved Hide resolved
2. React 将 `{name: 'Sara'}` 作为 props 传入并调用 `Welcome` 组件。
QC-L marked this conversation as resolved.
Show resolved Hide resolved
3. `Welcome` 组件将 `<h1>Hello, Sara</h1>` 元素作为返回值。
4. React DOM DOM 高效地更新为 `<h1>Hello, Sara</h1>`

>**Note:** Always start component names with a capital letter.
>**注意:** 组件名必须以大写字母开头。
QC-L marked this conversation as resolved.
Show resolved Hide resolved
>
>React treats components starting with lowercase letters as DOM tags. For example, `<div />` represents an HTML div tag, but `<Welcome />` represents a component and requires `Welcome` to be in scope.
>React 会将以小写字母开头的组件视为原生 DOM 标签。例如,`<div />` 代表 HTML div 标签,而 `<Welcome />` 则代表一个组件,且需在作用域内使用 `Welcome`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

且需在 => 并且会在

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得需在要好一些,这里是说只能在作用域内使用

>
>You can read more about the reasoning behind this convention [here.](/docs/jsx-in-depth.html#user-defined-components-must-be-capitalized)
>你可以在[此处](/docs/jsx-in-depth.html#user-defined-components-must-be-capitalized)了解有关此约定背后原因的更多信息。
QC-L marked this conversation as resolved.
Show resolved Hide resolved

## Composing Components
## 组合组件

Components can refer to other components in their output. This lets us use the same component abstraction for any level of detail. A button, a form, a dialog, a screen: in React apps, all those are commonly expressed as components.
组件可以在其输出中引用其他组件。这就可以让我们用同一组件来抽象出任意层次的细节。按钮,表单,对话框,甚至整个屏幕的内容:在 React 应用程序中,这些通常都会以组件的形式表示。

For example, we can create an `App` component that renders `Welcome` many times:
例如,我们可以创建 `App` 组件,用于多次渲染 `Welcome` 组件:
QC-L marked this conversation as resolved.
Show resolved Hide resolved

```js{8-10}
function Welcome(props) {
Expand All @@ -118,15 +118,15 @@ ReactDOM.render(
);
```

[](codepen://components-and-props/composing-components)
[在 CodePen 上试试](codepen://components-and-props/composing-components)

Typically, new React apps have a single `App` component at the very top. However, if you integrate React into an existing app, you might start bottom-up with a small component like `Button` and gradually work your way to the top of the view hierarchy.
通常来说,每个新的 React 应用程序的顶层组件都是 `App` 组件。但是,如果你将 React 集成到现有的应用程序中,你可能需要使用像 `Button` 这样的小组件,并自下而上地逐步应用到视图层的顶部。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

并自下而上地逐步应用到视图层的顶部 => 并自下而上地逐步替换到视图层的顶部
这里是“集成到现有的应用程序”,意思是从下至上的从小的组件开始替换。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

并自下而上地将这类组件逐步应用到视图层的每一处


## Extracting Components
## 提取组件

Don't be afraid to split components into smaller components.
将组件拆分为更小的组件。

For example, consider this `Comment` component:
例如,参考如下 `Comment` 组件:

```js
function Comment(props) {
Expand All @@ -152,13 +152,13 @@ function Comment(props) {
}
```

[](codepen://components-and-props/extracting-components)
[在 CodePen 上试试](codepen://components-and-props/extracting-components)

It accepts `author` (an object), `text` (a string), and `date` (a date) as props, and describes a comment on a social media website.
该组件接收 `author`(对象),`text` (字符串)以及 `date`(日期)作为 props,以此来描述社交媒体网站上的评论功能。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议:
它接收 author(对象),text (字符串)以及 date(日期)作为 props,这个组件用于描述一个社交媒体网站上的评论功能。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

反过来呢?

这个组件用于描述一个社交媒体网站上的评论功能,它接收 author(对象),text (字符串)以及 date(日期)作为 props


This component can be tricky to change because of all the nesting, and it is also hard to reuse individual parts of it. Let's extract a few components from it.
该组件由于嵌套的关系,变得难以维护,且很难复用它的某部分的代码。因此,我们需要从该组件中提取出一些组件。
QC-L marked this conversation as resolved.
Show resolved Hide resolved

First, we will extract `Avatar`:
首先,我们将提取 `Avatar` 组件:

```js{3-6}
function Avatar(props) {
Expand All @@ -171,11 +171,11 @@ function Avatar(props) {
}
```

The `Avatar` doesn't need to know that it is being rendered inside a `Comment`. This is why we have given its prop a more generic name: `user` rather than `author`.
`Avatar` `Comment` 组件内部,且不知道是否会被渲染。因此,我们给它的 props 起了一个更通用的名字:`user`,而不是 `author`
QC-L marked this conversation as resolved.
Show resolved Hide resolved

We recommend naming props from the component's own point of view rather than the context in which it is being used.
我们建议从组件自身的角度命名 props,而不能根据使用组件的上下文命名。
QC-L marked this conversation as resolved.
Show resolved Hide resolved

We can now simplify `Comment` a tiny bit:
我们现在针对 `Comment` 做些微小调整:

```js{5}
function Comment(props) {
Expand All @@ -198,7 +198,7 @@ function Comment(props) {
}
```

Next, we will extract a `UserInfo` component that renders an `Avatar` next to the user's name:
接下来,我们将提取 `UserInfo` 组件,该组件在用户名旁渲染 `Avatar` 组件:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用户名旁 => 用户名称旁边

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉有点长


```js{3-8}
function UserInfo(props) {
Expand All @@ -213,7 +213,7 @@ function UserInfo(props) {
}
```

This lets us simplify `Comment` even further:
进一步简化 `Comment` 组件:

```js{4}
function Comment(props) {
Expand All @@ -231,32 +231,32 @@ function Comment(props) {
}
```

[](codepen://components-and-props/extracting-components-continued)
[在 CodePen 上试用](codepen://components-and-props/extracting-components-continued)

Extracting components might seem like grunt work at first, but having a palette of reusable components pays off in larger apps. A good rule of thumb is that if a part of your UI is used several times (`Button`, `Panel`, `Avatar`), or is complex enough on its own (`App`, `FeedStory`, `Comment`), it is a good candidate to be a reusable component.
提取组件一开始看起来是件繁重的工作,但是,在大型应用中,构建可复用组件是完全值得的。根据经验来看,如果 UI 中有一部分被多次使用(`Button``Panel``Avatar`),或者组件本身就足够复杂(`App``FeedStory``Comment`),类似于这类的组件成为可复用组件是绝佳的选择。
QC-L marked this conversation as resolved.
Show resolved Hide resolved

## Props are Read-Only
## Props 的只读性

Whether you declare a component [as a function or a class](#function-and-class-components), it must never modify its own props. Consider this `sum` function:
组件无论是使用[函数式声明还是通过 class 声明](#function-and-class-components),都决不能修改自身的 props。来看下这个 `sum` 函数:

```js
function sum(a, b) {
return a + b;
}
```

Such functions are called ["pure"](https://en.wikipedia.org/wiki/Pure_function) because they do not attempt to change their inputs, and always return the same result for the same inputs.
这样的函数被称为[“纯函数”](https://en.wikipedia.org/wiki/Pure_function),因为该函数不会尝试更改入参,且相同的入参始终返回相同的结果。
QC-L marked this conversation as resolved.
Show resolved Hide resolved

In contrast, this function is impure because it changes its own input:
相反,下面这个函数则不是纯函数,因为它更改了自己的入参:

```js
function withdraw(account, amount) {
account.total -= amount;
}
```

React is pretty flexible but it has a single strict rule:
React 非常灵活,但它也有一个严格的规则:

**All React components must act like pure functions with respect to their props.**
**所有 React 组件都必须像纯函数一样保护它们的 props**
QC-L marked this conversation as resolved.
Show resolved Hide resolved

Of course, application UIs are dynamic and change over time. In the [next section](/docs/state-and-lifecycle.html), we will introduce a new concept of "state". State allows React components to change their output over time in response to user actions, network responses, and anything else, without violating this rule.
当然,应用程序的 UI 是动态的,并会伴随着时间的推移而变化。在[下一章节](/docs/state-and-lifecycle.html)中,我们将介绍一种新的概念,称之为 “state”。State 可以在不违反上述规则的情况下,根据用户操作、网络响应、或者其他状态变化,使组件动态的响应并改变组件的输出。
QC-L marked this conversation as resolved.
Show resolved Hide resolved