Skip to content

docs(cn): add Composition vs Inheritance translations #13

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

Merged
merged 3 commits into from
Feb 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions content/docs/composition-vs-inheritance.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
---
id: composition-vs-inheritance
title: Composition vs Inheritance
title: 组合 vs 继承
permalink: docs/composition-vs-inheritance.html
redirect_from:
- "docs/multiple-components.html"
prev: lifting-state-up.html
next: thinking-in-react.html
---

React has a powerful composition model, and we recommend using composition instead of inheritance to reuse code between components.
React 有十分强大的组合模式。我们推荐使用组合而非继承来实现组件间的代码重用。

In this section, we will consider a few problems where developers new to React often reach for inheritance, and show how we can solve them with composition.
在这篇文档中,我们将展示 React 初学者倾向于使用继承来解决的几个问题,并给出使用组合的思想来解决这些问题的方法。

## Containment
## 包含关系

Some components don't know their children ahead of time. This is especially common for components like `Sidebar` or `Dialog` that represent generic "boxes".
有些组件无法提前知晓它们的子组件。`Sidebar`(侧边栏)和 `Dialog`(对话框)等代表一般容器(box)的组件就是很好的例子。

We recommend that such components use the special `children` prop to pass children elements directly into their output:
我们建议这些组件使用一个特殊的 `children` prop 来将他们的子组件输出到渲染结果中:

```js{4}
function FancyBorder(props) {
Expand All @@ -28,7 +28,7 @@ function FancyBorder(props) {
}
```

This lets other components pass arbitrary children to them by nesting the JSX:
这使得别的组件可以通过 JSX 嵌套将任意组件作为子组件传递给它们。

```js{4-9}
function WelcomeDialog() {
Expand All @@ -45,11 +45,11 @@ function WelcomeDialog() {
}
```

**[Try it on CodePen](https://codepen.io/gaearon/pen/ozqNOV?editors=0010)**
**[CodePen 上尝试](https://codepen.io/gaearon/pen/ozqNOV?editors=0010)**

Anything inside the `<FancyBorder>` JSX tag gets passed into the `FancyBorder` component as a `children` prop. Since `FancyBorder` renders `{props.children}` inside a `<div>`, the passed elements appear in the final output.
`<FancyBorder>` JSX 标签中的所有内容都会作为一个 `children` prop 传递给 `FancyBorder` 组件。因为 `FancyBorder` `{props.children}` 渲染在一个 `<div>` 中,被传递的这些子组件最终都会出现在输出结果中。

While this is less common, sometimes you might need multiple "holes" in a component. In such cases you may come up with your own convention instead of using `children`:
有些情况下,你可能需要在一个组件中留出几个“洞”。尽管这种情况相对少见,但是你不需要拘泥于单一的 `children`,还可以自主设计相应的 props 和“洞”的位置来进行组件组合。

```js{5,8,18,21}
function SplitPane(props) {
Expand Down Expand Up @@ -78,15 +78,15 @@ function App() {
}
```

[**Try it on CodePen**](https://codepen.io/gaearon/pen/gwZOJp?editors=0010)
[**CodePen 上尝试**](https://codepen.io/gaearon/pen/gwZOJp?editors=0010)

React elements like `<Contacts />` and `<Chat />` are just objects, so you can pass them as props like any other data. This approach may remind you of "slots" in other libraries but there are no limitations on what you can pass as props in React.
`<Contacts />` `<Chat />` 之类的 React 元素本质就是对象(object),所以你可以把它们当作 props,像其他数据一样传递。这种方法可能使你想起别的库中“槽”(slot)的概念,但在 React 中你可以将任何东西作为 props 进行传递,没有限制。

## Specialization
## 特例关系

Sometimes we think about components as being "special cases" of other components. For example, we might say that a `WelcomeDialog` is a special case of `Dialog`.
有些时候,我们会把一些组件看作是其他组件的特殊实例,比如 `WelcomeDialog` 可以说是 `Dialog` 的特殊实例。

In React, this is also achieved by composition, where a more "specific" component renders a more "generic" one and configures it with props:
在 React 中,我们也可以通过组合来实现这一点。“特殊”组件可以通过 props 定制并渲染“一般”组件:

```js{5,8,16-18}
function Dialog(props) {
Expand All @@ -111,9 +111,9 @@ function WelcomeDialog() {
}
```

[**Try it on CodePen**](https://codepen.io/gaearon/pen/kkEaOZ?editors=0010)
[**CodePen 上尝试**](https://codepen.io/gaearon/pen/kkEaOZ?editors=0010)

Composition works equally well for components defined as classes:
组合也同样适用于以 class 形式定义的组件。

```js{10,27-31}
function Dialog(props) {
Expand Down Expand Up @@ -161,12 +161,12 @@ class SignUpDialog extends React.Component {
}
```

[**Try it on CodePen**](https://codepen.io/gaearon/pen/gwZbYa?editors=0010)
[**CodePen 上尝试**](https://codepen.io/gaearon/pen/gwZbYa?editors=0010)

## So What About Inheritance?
## 那么继承呢?

At Facebook, we use React in thousands of components, and we haven't found any use cases where we would recommend creating component inheritance hierarchies.
Facebook,我们在成百上千个组件中使用 React。我们并没有发现需要使用继承来构建组件层次的情况。

Props and composition give you all the flexibility you need to customize a component's look and behavior in an explicit and safe way. Remember that components may accept arbitrary props, including primitive values, React elements, or functions.
Props 和组合为你提供了清晰而安全地定制组件外观和行为的灵活方式。注意:组件可以接受任意 props,包括基本数据类型,React 元素以及函数。

If you want to reuse non-UI functionality between components, we suggest extracting it into a separate JavaScript module. The components may import it and use that function, object, or a class, without extending it.
如果你想要在组件间复用非 UI 的功能,我们建议将其提取为一个单独的 JavaScript 模块,如函数、对象或者类。组件可以直接引入(import)而无需扩展它们。