You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Khai báo Props mặc định {#declaring-default-props}
32
32
33
-
With functions and ES6 classes`defaultProps`is defined as a property on the component itself:
33
+
Với các function và các ES6 class,`defaultProps`được định nghĩa như là một property trên chính component:
34
34
35
35
```javascript
36
36
classGreetingextendsReact.Component {
@@ -42,7 +42,7 @@ Greeting.defaultProps = {
42
42
};
43
43
```
44
44
45
-
With`createReactClass()`, you need to define`getDefaultProps()`as a function on the passed object:
45
+
Với`createReactClass()`, bạn cần xác định`getDefaultProps()`như là một function trên đối tượng được truyền:
46
46
47
47
```javascript
48
48
var Greeting =createReactClass({
@@ -57,9 +57,9 @@ var Greeting = createReactClass({
57
57
});
58
58
```
59
59
60
-
## Setting the Initial State {#setting-the-initial-state}
60
+
## Cài đặt State ban đầu {#setting-the-initial-state}
61
61
62
-
In ES6 classes, you can define the initial state by assigning `this.state`in the constructor:
62
+
Trong các ES6 class, bạn cần định nghĩa state ban đầu bằng cách gán `this.state`bên trong constructor:
63
63
64
64
```javascript
65
65
classCounterextendsReact.Component {
@@ -71,7 +71,7 @@ class Counter extends React.Component {
71
71
}
72
72
```
73
73
74
-
With`createReactClass()`, you have to provide a separate `getInitialState` method that returns the initial state:
74
+
Với`createReactClass()`, bạn phải cung cấp một `getInitialState` method trả về state ban đầu::
75
75
76
76
```javascript
77
77
var Counter =createReactClass({
@@ -84,7 +84,7 @@ var Counter = createReactClass({
84
84
85
85
## Autobinding {#autobinding}
86
86
87
-
In React components declared as ES6 classes, methods follow the same semantics as regular ES6 classes. This means that they don't automatically bind `this`to the instance. You'll have to explicitly use `.bind(this)`in the constructor:
87
+
Trong các React component được khai báo như là các ES6 class, các method tuân theo ngữ nghĩa giống như các ES6 class thông thường. Điều này có nghĩa là chúng không tự động bind `this`đến instance. Bạn sẽ phải sử dụng rõ ràng `.bind(this)`bên trong constructor:
88
88
89
89
```javascript
90
90
classSayHelloextendsReact.Component {
@@ -110,7 +110,7 @@ class SayHello extends React.Component {
110
110
}
111
111
```
112
112
113
-
With`createReactClass()`, this is not necessary because it binds all methods:
113
+
Với`createReactClass()`, điều này là không cần thiết vì nó sẽ bind tất cả các method:
114
114
115
115
```javascript
116
116
var SayHello =createReactClass({
@@ -132,9 +132,9 @@ var SayHello = createReactClass({
132
132
});
133
133
```
134
134
135
-
This means writing ES6 classes comes with a little more boilerplate code for event handlers, but the upside is slightly better performance in large applications.
135
+
Điều này có nghĩa là viết các ES6 class đi kèm với một chút code soạn sẵn cho các trình xử lý event, nhưng mặt trái của nó là performance tốt hơn một chút trong các ứng dụng lớn.
136
136
137
-
If the boilerplate code is too unattractive to you, you may enable the**experimental**[Class Properties](https://babeljs.io/docs/plugins/transform-class-properties/) syntax proposal with Babel:
137
+
Nếu code soạn sẵn quá không hấp dẫn đối với bạn, bạn có thể bật**experimental**[Class Properties](https://babeljs.io/docs/plugins/transform-class-properties/) syntax proposal với Babel:
138
138
139
139
140
140
```javascript
@@ -159,27 +159,27 @@ class SayHello extends React.Component {
159
159
}
160
160
```
161
161
162
-
Please note that the syntax above is **experimental**and the syntax may change, or the proposal might not make it into the language.
162
+
Xin lưu ý rằng cú pháp ở trên là **experimental**và cú pháp có thể thay đổi, hoặc đề xuất có thể không biến nó thành ngôn ngữ.
163
163
164
-
If you'd rather play it safe, you have a few options:
164
+
Nếu bạn muốn an toàn hơn, bạn có thể có một số lựa chọn bên dưới:
165
165
166
-
* Bind methods in the constructor.
167
-
*Use arrow functions, e.g. `onClick={(e) => this.handleClick(e)}`.
168
-
*Keep using`createReactClass`.
166
+
* Bind methods bên trong constructor.
167
+
*Sử dụng arrow functions, e.g. `onClick={(e) => this.handleClick(e)}`.
168
+
*Tiếp tục sử dụng`createReactClass`.
169
169
170
170
## Mixins {#mixins}
171
171
172
-
>**Note:**
172
+
>**Ghi chú:**
173
173
>
174
-
>ES6 launched without any mixin support. Therefore, there is no support for mixins when you use React with ES6 classes.
174
+
>ES6 ra mắt mà không có bất kỳ hỗ trợ mixin nào. Do đó, sẽ không có hỗ trợ cho các mixin khi bạn sử dụng React với các ES6 class.
175
175
>
176
-
>**We also found numerous issues in codebases using mixins, [and don't recommend using them in the new code](/blog/2016/07/13/mixins-considered-harmful.html).**
176
+
>**Chúng tôi cũng tìm thấy nhiều vấn đề trong các codebase sử dụng các mixin [và không khuyên bạn sử dụng chúng trong code mới](/blog/2016/07/13/mixins-considered-harmful.html).**
177
177
>
178
-
>This section exists only for the reference.
178
+
>Phần này chỉ để tham khảo.
179
179
180
-
Sometimes very different components may share some common functionality. These are sometimes called [cross-cutting concerns](https://en.wikipedia.org/wiki/Cross-cutting_concern). `createReactClass`lets you use a legacy `mixins`system for that.
180
+
Đôi khi các component rất khác nhau có thể chia sẻ một số chức năng chung. Đây đôi khi được gọi là [cross-cutting concerns](https://en.wikipedia.org/wiki/Cross-cutting_concern). `createReactClass`cho phép bạn sử dụng hệ thống `mixins`kế thừa cho việc đó.
181
181
182
-
One common use case is a component wanting to update itself on a time interval. It's easy to use `setInterval()`, but it's important to cancel your interval when you don't need it anymore to save memory. React provides [lifecycle methods](/docs/react-component.html#the-component-lifecycle)that let you know when a component is about to be created or destroyed. Let's create a simple mixin that uses these methods to provide an easy `setInterval()` function that will automatically get cleaned up when your component is destroyed.
182
+
Một trường hợp sử dụng phổ biến là một component muốn tự cập nhật trong một khoảng thời gian (time interval). Thật dễ dàng để sử dụng `setInterval()`, nhưng điều quan trọng là phải hủy interval của bạn khi bạn không cần nó nữa để tiết kiệm bộ nhớ. React cung cấp các [lifecycle methods](/docs/react-component.html#the-component-lifecycle)cho bạn biết khi nào một component sắp được tạo hoặc bị phá hủy. Hãy tạo một mixin đơn giản sử dụng các method này để cung cấp một `setInterval()` function dễ dàng sẽ tự động được dọn dẹp khi component của bạn bị phá hủy.
183
183
184
184
```javascript
185
185
var SetIntervalMixin = {
@@ -222,4 +222,4 @@ ReactDOM.render(
222
222
);
223
223
```
224
224
225
-
If a component is using multiple mixins and several mixins define the same lifecycle method (i.e. several mixins want to do some cleanup when the component is destroyed), all of the lifecycle methods are guaranteed to be called. Methods defined on mixins run in the order mixins were listed, followed by a method call on the component.
225
+
Nếu một component đang sử dụng nhiều mixin và một số mixin xác định cùng một lifecycle method (tức là một số mixin muốn thực hiện một số dọn dẹp khi component bị phá hủy), tất cả các lifecycle method được đảm bảo sẽ được gọi. Các method được xác định trên các mixin chạy theo thứ tự các mixin được liệt kê, theo sau là một lệnh gọi method trên component.
0 commit comments