Skip to content

Commit bfcc8ea

Browse files
robertyingQC-L
authored andcommitted
docs(cn): add Composition vs Inheritance translations (#13)
* docs(cn): add `Composition vs Inheritance` translations * docs(cn): add missing �colon * Update composition-vs-inheritance.md
1 parent 9c4e906 commit bfcc8ea

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

Diff for: content/docs/composition-vs-inheritance.md

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
---
22
id: composition-vs-inheritance
3-
title: Composition vs Inheritance
3+
title: 组合 vs 继承
44
permalink: docs/composition-vs-inheritance.html
55
redirect_from:
66
- "docs/multiple-components.html"
77
prev: lifting-state-up.html
88
next: thinking-in-react.html
99
---
1010

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

13-
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.
13+
在这篇文档中,我们将展示 React 初学者倾向于使用继承来解决的几个问题,并给出使用组合的思想来解决这些问题的方法。
1414

15-
## Containment {#containment}
15+
## 包含关系 {#containment}
1616

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

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

2121
```js{4}
2222
function FancyBorder(props) {
@@ -28,7 +28,7 @@ function FancyBorder(props) {
2828
}
2929
```
3030

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

3333
```js{4-9}
3434
function WelcomeDialog() {
@@ -45,11 +45,11 @@ function WelcomeDialog() {
4545
}
4646
```
4747

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

50-
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.
50+
`<FancyBorder>` JSX 标签中的所有内容都会作为一个 `children` prop 传递给 `FancyBorder` 组件。因为 `FancyBorder` `{props.children}` 渲染在一个 `<div>` 中,被传递的这些子组件最终都会出现在输出结果中。
5151

52-
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`:
52+
有些情况下,你可能需要在一个组件中留出几个“洞”。尽管这种情况相对少见,但是你不需要拘泥于单一的 `children`,还可以自主设计相应的 props 和“洞”的位置来进行组件组合。
5353

5454
```js{5,8,18,21}
5555
function SplitPane(props) {
@@ -78,15 +78,15 @@ function App() {
7878
}
7979
```
8080

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

83-
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.
83+
`<Contacts />` `<Chat />` 之类的 React 元素本质就是对象(object),所以你可以把它们当作 props,像其他数据一样传递。这种方法可能使你想起别的库中“槽”(slot)的概念,但在 React 中你可以将任何东西作为 props 进行传递,没有限制。
8484

85-
## Specialization {#specialization}
85+
## 特例关系 {#specialization}
8686

87-
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`.
87+
有些时候,我们会把一些组件看作是其他组件的特殊实例,比如 `WelcomeDialog` 可以说是 `Dialog` 的特殊实例。
8888

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

9191
```js{5,8,16-18}
9292
function Dialog(props) {
@@ -111,9 +111,9 @@ function WelcomeDialog() {
111111
}
112112
```
113113

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

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

118118
```js{10,27-31}
119119
function Dialog(props) {
@@ -161,12 +161,12 @@ class SignUpDialog extends React.Component {
161161
}
162162
```
163163

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

166-
## So What About Inheritance? {#so-what-about-inheritance}
166+
## 那么继承呢? {#so-what-about-inheritance}
167167

168-
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.
168+
Facebook,我们在成百上千个组件中使用 React。我们并没有发现需要使用继承来构建组件层次的情况。
169169

170-
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.
170+
Props 和组合为你提供了清晰而安全地定制组件外观和行为的灵活方式。注意:组件可以接受任意 props,包括基本数据类型,React 元素以及函数。
171171

172-
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.
172+
如果你想要在组件间复用非 UI 的功能,我们建议将其提取为一个单独的 JavaScript 模块,如函数、对象或者类。组件可以直接引入(import)而无需扩展它们。

0 commit comments

Comments
 (0)