Skip to content

Commit a4ef7f5

Browse files
authored
Merge pull request #32 from cuongtran8/feature/component-vs-inderitance
Translate page composition vs inheritance
2 parents 3c58151 + 127841e commit a4ef7f5

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

content/docs/composition-vs-inheritance.md

+23-23
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: Kết hợp và kế thừa
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 có một mô hình kết hợp mạnh mẽ, khuyến khích sử dụng tính kết hợp hơn là kế thừa để tái sử dụng code giữa các component.
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+
Trong phần này, chúng ta sẽ xem xét một vài vấn đề với những người mới bắt đầu với React, họ thường sử dụng kế thừa, và đưa ra cách giải quyết vấn đề đó với tính kế thừa.
1414

15-
## Containment {#containment}
15+
## Giới hạn {#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+
Một vài component không biết về các component con của nó trước thời hạn. Điều này rất phổ biến với những component như `Sidebar` `Dialog` đóng vài trò như là những chiếc "hộp" chung.
1818

19-
We recommend that such components use the special `children` prop to pass children elements directly into their output:
19+
Chúng tôi khuyến khích sử dụng những prop `con` đặc biệt để truyền những element con trực tiếp tới đầu ra của các component này:
2020

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

31-
This lets other components pass arbitrary children to them by nesting the JSX:
31+
Nó giúp cho các component khác truyền những element con một cách linh động hơn bằng cách lồng JSX với nhau:
3232

3333
```js{4-9}
34-
function WelcomeDialog() {
34+
function WelcomeDialog() {vào
3535
return (
3636
<FancyBorder color="blue">
3737
<h1 className="Dialog-title">
@@ -45,11 +45,11 @@ function WelcomeDialog() {
4545
}
4646
```
4747

48-
**[Try it on CodePen](https://codepen.io/gaearon/pen/ozqNOV?editors=0010)**
48+
**[Xem ví dụ trên 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+
Tất cả một thứ trong thẻ JSX `<FancyBorder>` được truyền vào trong `FancyBorder` component như là một `children` prop. `FancyBorder` tạo ra `{props.children}` bên trong thẻ `<div>`, nên các phần tử được truyền vào cuối cùng sẽ xuất hiện tại đầu ra.
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+
Khi nó trở nên ít phổ biến hơn, đôi khi bạn có thể cần tới nhiều chỗ trống trong một component. Trong trường hợp như thế bạn có thể tạo ra những quy ước của riêng mình thay vì sử dụng `children`:
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+
[**Xem ví dụ trên 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+
Các phần tử React như `<Contacts />` `<Chat />` là các đối tượng, nên bạn có thể truyền nó như là props tương tự như các dữ liệu khác. Phương pháp này có thể nhắc bạn về khái niệm "slots" trong các thư viện khác nhưng không hề có một giới hạn nào với các tham số có thể truyền như props trong React.
8484

85-
## Specialization {#specialization}
85+
## Chuyên biệt hoá {#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+
Đôi khi chúng ta nghĩ về các component như là "một trường hợp đặc biệt" của các component khác. Ví dụ, chúng ta có thể nói rằng `WelcomeDialog` là một trường hợp đặc biệt của `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+
Trong React, nó có thể đạt được bằng cách kết hợp, khi gộp nhiều component "đặc biệt" để tạo ra một component chung và cấu hình nó với 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+
[**Xem ví dụ trên CodePen**](https://codepen.io/gaearon/pen/kkEaOZ?editors=0010)
115115

116-
Composition works equally well for components defined as classes:
116+
Phương thức kết hợp hoạt động tốt cho cả các component định nghĩa như là những 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+
[**Xem ví dụ trên CodePen**](https://codepen.io/gaearon/pen/gwZbYa?editors=0010)
165165

166-
## So What About Inheritance? {#so-what-about-inheritance}
166+
## Vậy còn tính kế thừa thì sao? {#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+
Tại Facebook, chúng tôi sử dụng React trong hàng ngàn các components, và chúng tôi không thấy một trường hợp nào chúng tôi khuyến khích tạo ra hệ thống component kế thừa.
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 và tính kết hợp mang lại sự linh hoạt mà bạn cần để tuỳ chỉnh giao hiện và hành vi một cách rõ ràng và an toàn. Nhớ rằng các component có thể chấp nhận các props không giới hạn, kể cả các giá trị sơ khai, các phần tử React hoặc các hàm.
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+
Nếu bạn muốn tái sử dụng các chức nằng không liên quan tới giao diện người dùng, chúng tôi khuyến khích nên tách biệt nó ra những module Javascript riêng. Các component có thể nhập nó và sử dụng các hàm, đối tượng hoặc class, mà không phải mở rộng nó.

0 commit comments

Comments
 (0)