Skip to content

Commit a012cfe

Browse files
authored
Merge pull request #29 from a-deeb/master
Translating 'composition-vs-inheritance.md'
2 parents 9def0e9 + 2331793 commit a012cfe

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

โ€Žcontent/docs/composition-vs-inheritance.md

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
---
22
id: composition-vs-inheritance
3-
title: Composition vs Inheritance
3+
title: ุงู„ูุฑู‚ ุจูŠู† ุงู„ุชุฑูƒูŠุจ ูˆุงู„ูˆุฑุงุซุฉ ููŠ React
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 ู†ู…ูˆุฐุฌู‹ุง ู‚ูˆูŠู‹ู‘ุง ู„ู„ุชุฑูƒูŠุจ (composition) ูˆู†ููˆุตูŠ ุจุงุณุชุฎุฏุงู… ุงู„ุชุฑูƒูŠุจ ุจุฏู„ู‹ุง ู…ู† ุงู„ูˆุฑุงุซุฉ (inheritance) ู„ุฅุนุงุฏุฉ ุงุณุชุฎุฏุงู… ุงู„ุดูŠูุฑุฉ ุจูŠู† ุงู„ู…ููƒูˆูู‘ู†ุงุช.
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}
1615

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".
16+
## ู…ูู‡ูˆู… ุงู„ุงุญุชูˆุงุก {#containment}
17+
18+
19+
ู„ุง ุชุนุฑู ุจุนุถ ุงู„ู…ููƒูˆูู‘ู†ุงุช ุงู„ุนู†ุงุตุฑ ุงู„ุฃุจู†ุงุก ู„ู‡ุง ู…ูุณุจู‚ู‹ุงุŒ ูˆู‡ูˆ ุฃู…ุฑูŒ ุดุงุฆุนูŒ ุจุดูƒู„ู ุฎุงุต ุจุงู„ู†ุณุจุฉ ู„ู…ููƒูˆูู‘ู† ุงู„ู‚ุงุฆู…ุฉ ุงู„ุฌุงู†ุจูŠู‘ุฉ `Sidebar` ุฃูˆ ู…ุฑุจู‘ุน ุงู„ุญูˆุงุฑ `Dialog`.
20+
21+
ู†ููˆุตูŠ ุจุฃู† ุชุณุชุฎุฏู… ุชู„ูƒ ุงู„ู…ููƒูˆูู‘ู†ุงุช ุงู„ุฎุงุตูŠู‘ุฉ `children` ู„ุชู…ุฑูŠุฑ ุงู„ุนู†ุงุตุฑ ุจุดูƒู„ู ู…ุจุงุดุฑ ุฅู„ู‰ ู†ุงุชุฌู‡ุง:
1822

19-
We recommend that such components use the special `children` prop to pass children elements directly into their output:
2023

2124
```js{4}
2225
function FancyBorder(props) {
@@ -27,8 +30,7 @@ function FancyBorder(props) {
2730
);
2831
}
2932
```
30-
31-
This lets other components pass arbitrary children to them by nesting the JSX:
33+
ูŠูุชูŠุญ ู‡ุฐุง ู„ู„ู…ููƒูˆูู‘ู†ุงุช ุงู„ุฃุฎุฑู‰ ุจุฃู† ุชูู…ุฑูู‘ุฑ ุนู†ุงุตุฑ ุฃุจู†ุงุก ู„ู‡ุง ุนู† ุทุฑูŠู‚ ุงู„ุชุฏุงุฎู„ ููŠ JSX:
3234

3335
```js{4-9}
3436
function WelcomeDialog() {
@@ -45,11 +47,12 @@ function WelcomeDialog() {
4547
}
4648
```
4749

48-
**[Try it on CodePen](https://codepen.io/gaearon/pen/ozqNOV?editors=0010)**
50+
**[ุฌุฑุจ ุงู„ู…ุซุงู„ ุนู„ู‰ CodePen](https://codepen.io/gaearon/pen/ozqNOV?editors=0010)**
4951

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.
52+
ูŠูู…ุฑูŽู‘ุฑ ุฃูŠ ุดูŠุก ุจุฏุงุฎู„ ุงู„ุนู†ุตุฑ `<FancyBorder>` ุฅู„ู‰ ุงู„ู…ููƒูˆูู‘ู† `FancyBorder` ุนุจุฑ ุงู„ุฎุงุตูŠู‘ุฉ `children`. ูˆุจู…ุง ุฃู†ู‘ ุงู„ู…ููƒูˆูู‘ู† `FancyBorder` ูŠูุตูŠูู‘ุฑ `{props.children}` ุจุฏุงุฎู„ ุนู†ุตุฑ `<div>`ุŒ ูุณุชุธู‡ุฑ ุงู„ุนู†ุงุตุฑ ุงู„ู…ูู…ุฑูŽู‘ุฑุฉ ุจุฏุงุฎู„ ุงู„ู†ุงุชุฌ ุงู„ู†ู‡ุงุฆูŠ.
5153

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`:
54+
ุฃุญูŠุงู†ู‹ุง ู‚ุฏ ุชุญุชุงุฌ ุฅู„ู‰ ุชู…ุฑูŠุฑ ุนู†ุงุตุฑ ู…ูุชุนุฏูู‘ุฏุฉ. ููŠ ุชู„ูƒ ุงู„ุญุงู„ุงุช ูŠุฌุจ ุฃู† ุชูุนู„ ุฐู„ูƒ ุจุทุฑูŠู‚ุชูƒ ุงู„ุฎุงุตู‘ุฉ ุจุฏู„ู‹ุง ู…ู† ุงุณุชุฎุฏุงู… `childrenุŒ` ูƒู…ุง ูŠู„ูŠ:
55+
5356

5457
```js{5,8,18,21}
5558
function SplitPane(props) {
@@ -78,15 +81,16 @@ function App() {
7881
}
7982
```
8083

81-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/gwZOJp?editors=0010)
84+
[**ุฌุฑุจ ุงู„ู…ุซุงู„ ุนู„ู‰ CodePen**](https://codepen.io/gaearon/pen/gwZOJp?editors=0010)
85+
86+
ุฅู†ู‘ ุนู†ุงุตุฑ React ู…ุซู„ โ€Ž`<Contacts />โ€Ž` ูˆ โ€Ž`<Chat />โ€Ž` ู‡ูŠ ู…ูุฌุฑู‘ุฏ ูƒุงุฆู†ุงุชุŒ ู„ุฐู„ูƒ ุจุฅู…ูƒุงู†ูƒ ุชู…ุฑูŠุฑู‡ุง ูƒุฎุงุตูŠู‘ุงุช `props` ู…ุซู„ ุฃูŠ ุจูŠุงู†ุงุช ุฃุฎุฑู‰. ู‚ุฏ ูŠูุฐูƒูู‘ุฑูƒ ุฐู„ูƒ ุจู…ูู‡ูˆู… ุงู„ู…ุฏุงุฎู„ (slots) ููŠ ู…ูƒุชุจุงุช ุฃุฎุฑู‰ุŒ ูˆู„ูƒู† ู„ุง ุชูˆุฌุฏ ุญุฏูˆุฏ ู„ู…ุง ูŠูู…ูƒูู†ูƒ ุชู…ุฑูŠุฑู‡ ูƒุฎุงุตูŠู‘ุงุช `props` ููŠ React.
8287

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.
8488

85-
## Specialization {#specialization}
89+
## ุงู„ุชุฎุตูŠุต {#specialization}
8690

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`.
91+
ุฃุญูŠุงู†ู‹ุง ู†ูููƒู‘ุฑ ุจุงู„ู…ููƒูˆูู‘ู†ุงุช ุนู„ู‰ ุฃู†ู‘ู‡ุง ุญุงู„ุงุช ุฎุงุตู‘ุฉ ู„ู…ููƒูˆูู‘ู†ุงุช ุฃุฎุฑู‰ุŒ ูู…ุซู„ู‹ุง ู‚ุฏ ู†ู‚ูˆู„ ุฃู†ู‘ ู…ููƒูˆูู‘ู† ู…ุฑุจู‘ุน ุงู„ุชุฑุญูŠุจ `WelcomeDialog` ู‡ูˆ ุญุงู„ุฉ ุฎุงุตู‘ุฉ ู…ู† ู…ุฑุจู‘ุน ุงู„ุญูˆุงุฑ `Dialog`.
8892

89-
In React, this is also achieved by composition, where a more "specific" component renders a more "generic" one and configures it with props:
93+
ูŠูู…ูƒูู† ุชุญู‚ูŠู‚ ุฐู„ูƒ ููŠ React ุนู† ุทุฑูŠู‚ ุงุณุชุฎุฏุงู… ุงู„ุชุฑูƒูŠุจ (composition) ุญูŠุซ ูŠูุตูŠูู‘ุฑ ุงู„ู…ููƒูˆูู‘ู† ุงู„ุฃูƒุซุฑ ุฎุตูˆุตูŠู‘ุฉ ุงู„ู…ููƒูˆูู‘ู† ุงู„ุฃูƒุซุฑ ุนู…ูˆู…ูŠู‘ุฉ ูˆูŠูุนุฏ ู„ู‡ ุงู„ุฎุงุตูŠู‘ุงุช:
9094

9195
```js{5,8,16-18}
9296
function Dialog(props) {
@@ -111,9 +115,9 @@ function WelcomeDialog() {
111115
}
112116
```
113117

114-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/kkEaOZ?editors=0010)
118+
[**ุฌุฑุจ ุงู„ู…ุซุงู„ ุนู„ู‰ CodePen**](https://codepen.io/gaearon/pen/kkEaOZ?editors=0010)
115119

116-
Composition works equally well for components defined as classes:
120+
ูŠุนู…ู„ ุงู„ุชุฑูƒูŠุจ ุจุดูƒู„ู ู…ูู…ุงุซู„ ู„ู„ู…ููƒูˆูู‘ู†ุงุช ุงู„ู…ูุนุฑูŽู‘ูุฉ ูƒุฃุตู†ุงู:
117121

118122
```js{10,27-31}
119123
function Dialog(props) {
@@ -161,12 +165,13 @@ class SignUpDialog extends React.Component {
161165
}
162166
```
163167

164-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/gwZbYa?editors=0010)
168+
[**ุฌุฑุจ ุงู„ู…ุซุงู„ ุนู„ู‰ CodePen**](https://codepen.io/gaearon/pen/gwZbYa?editors=0010)
169+
165170

166-
## So What About Inheritance? {#so-what-about-inheritance}
171+
## ุฅุฐู‹ุง ู…ุงุฐุง ุนู† ุงู„ูˆุฑุงุซุฉุŸ {#so-what-about-inheritance}
167172

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.
173+
ู†ุณุชุฎุฏู… ููŠ ููŠุณุจูˆูƒ ุขู„ุงู ู…ููƒูˆูู‘ู†ุงุช ReactุŒ ูˆู„ู… ู†ุฌุฏ ุฃูŠ ุญุงู„ุฉ ู†ููุถูู‘ู„ ููŠู‡ุง ุงุณุชุฎุฏุงู… ุงู„ูˆุฑุงุซุฉ.
169174

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.
175+
ูŠู…ู†ุญูƒ ุงู„ุชุฑูƒูŠุจ ูˆุงู„ุฎุงุตูŠู‘ุงุช `props` ุงู„ู…ุฑูˆู†ุฉ ุงู„ุชูŠ ุชุญุชุงุฌู‡ุง ู„ุชุฎุตูŠุต ู…ุธู‡ุฑ ูˆุณู„ูˆูƒ ุงู„ู…ููƒูˆูู‘ู†ุงุช ุจุทุฑูŠู‚ุฉ ู…ุถุจูˆุทุฉ ูˆุขู…ู†ุฉ. ุชุฐูƒู‘ุฑ ุฃู†ู‘ ุงู„ู…ููƒูˆู‘ู†ุงุช ู‚ุฏ ุชุณุชู‚ุจู„ ุฎุงุตูŠู‘ุงุช ู…ู† ู…ุญุชูˆู‰ ู…ูุชุนุฏูู‘ุฏุŒ ู…ุซู„ ุงู„ู‚ูŠู… ุงู„ู…ุจุฏุฆูŠุฉุŒ ูˆุนู†ุงุตุฑ ReactุŒ ูˆุงู„ุฏูˆุงู„.
171176

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.
177+
ุฅู† ุฃุฑุฏุช ุฅุนุงุฏุฉ ุงุณุชุฎุฏุงู… ุจุนุถ ุงู„ูˆุธุงุฆู ุจูŠู† ุงู„ู…ููƒูˆูู‘ู†ุงุช ุบูŠุฑ ุงู„ู…ูุชุนู„ูู‘ู‚ุฉ ุจูˆุงุฌู‡ุฉ ุงู„ู…ุณุชุฎุฏู… ูู†ู‚ุชุฑุญ ุงุณุชุฎุฑุงุฌู‡ุง ุฅู„ู‰ ูˆุงุญุฏุงุช JavaScript ู…ูู†ูุตูู„ุฉุŒ ุญูŠุซ ูŠูู…ูƒูู† ู„ู„ู…ููƒูˆูู‘ู† ุฃู† ูŠุณุชูˆุฑุฏ ูˆูŠุณุชุฎุฏู… ุงู„ุฏูˆุงู„ ูˆุงู„ูƒุงุฆู†ุงุช ูˆุงู„ุฃุตู†ุงู ุจุฏูˆู† ุงู„ุงู…ุชุฏุงุฏ ู„ู‡ุง ุนู† ุทุฑูŠู‚ ุงู„ูƒู„ู…ุฉ `extend`.

0 commit comments

Comments
ย (0)