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
Copy file name to clipboardExpand all lines: content/docs/faq-functions.md
+44-44Lines changed: 44 additions & 44 deletions
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,26 @@
1
1
---
2
2
id: faq-functions
3
-
title: Passing Functions to Components
3
+
title: Truyền Functions cho Component
4
4
permalink: docs/faq-functions.html
5
5
layout: docs
6
6
category: FAQ
7
7
---
8
8
9
-
### How do I pass an event handler (like onClick) to a component?{#how-do-i-pass-an-event-handler-like-onclick-to-a-component}
9
+
### Làm thế nào tôi truyền một sự kiện (như onClick) cho một component?{#how-do-i-pass-an-event-handler-like-onclick-to-a-component}
10
10
11
-
Pass event handlers and other functions as props to child components:
11
+
Truyền một sự kiện xử lý và một function khác như là một props cho component con:
12
12
13
13
```jsx
14
14
<button onClick={this.handleClick}>
15
15
```
16
16
17
-
If you need to have access to the parent component in the handler, you also need to bind the function to the component instance (see below).
17
+
Nếu bạn cần truy cập vào component cha trong xử lý, bạn cần phải bind function cụ thể với một component (xem bên dưới).
18
18
19
-
### How do I bind a function to a component instance? {#how-do-i-bind-a-function-to-a-component-instance}
19
+
### Làm sao tôi bind một function cho component cụ thể? {#how-do-i-bind-a-function-to-a-component-instance}
20
20
21
-
There are several ways to make sure functions have access to component attributes like `this.props`and`this.state`, depending on which syntax and build steps you are using.
21
+
Có một số cách để đảm bảo các function có quyền truy cập vào các thuộc tính component như `this.props`và`this.state`, tùy thuộc vào cú pháp và các bước xây dựng mà bạn đang sử dụng.
22
22
23
-
#### Bind in Constructor (ES2015) {#bind-in-constructor-es2015}
23
+
#### Bind trong Constructor (ES2015) {#bind-in-constructor-es2015}
24
24
25
25
```jsx
26
26
classFooextendsComponent {
@@ -51,7 +51,7 @@ class Foo extends Component {
51
51
}
52
52
```
53
53
54
-
#### Bind in Render {#bind-in-render}
54
+
#### Bind trong Render {#bind-in-render}
55
55
56
56
```jsx
57
57
classFooextendsComponent {
@@ -64,11 +64,11 @@ class Foo extends Component {
64
64
}
65
65
```
66
66
67
-
>**Note:**
67
+
>**Lưu ý:**
68
68
>
69
-
>Using `Function.prototype.bind`in render creates a new function each time the component renders, which may have performance implications (see below).
69
+
>Sử dụng `Function.prototype.bind`trong hàm render tạo ra một function mới mỗi lần component renders, điều đó có thể liên quan tới hiệu suất (Xem bên dưới).
70
70
71
-
#### Arrow Function in Render {#arrow-function-in-render}
71
+
#### Arrow Function trong Render {#arrow-function-in-render}
72
72
73
73
```jsx
74
74
classFooextendsComponent {
@@ -81,19 +81,19 @@ class Foo extends Component {
81
81
}
82
82
```
83
83
84
-
>**Note:**
84
+
>**Lưu ý:**
85
85
>
86
-
>Using an arrow function in render creates a new function each time the component renders, which may break optimizations based on strict identity comparison.
86
+
>Sử dụng arrow function trong hàm render tạo ra một function mới mỗi lần component renders, điều đó làm phá vỡ tính tối ưu dựa trên so sánh các định danh.
87
87
88
-
### Is it OK to use arrow functions in render methods? {#is-it-ok-to-use-arrow-functions-in-render-methods}
88
+
### Có được sử dụng arrow function trong hàm render không? {#is-it-ok-to-use-arrow-functions-in-render-methods}
89
89
90
-
Generally speaking, yes, it is OK, and it is often the easiest way to pass parameters to callback functions.
90
+
Nói chung, đồng ý, nó OK, và nó thường là cách dễ nhất để truyền tham số cho các callback functions.
91
91
92
-
If you do have performance issues, by all means, optimize!
92
+
Nếu bạn có vấn đề về hiệu suất, bằng mọi cách, hãy tối ưu hóa!
93
93
94
-
### Why is binding necessary at all? {#why-is-binding-necessary-at-all}
94
+
### Tại sao ràng buộc cần thiết ở tất cả? {#why-is-binding-necessary-at-all}
95
95
96
-
In JavaScript, these two code snippets are **not**equivalent:
96
+
Trong JavaScript, hai đoạn code này **không**tương đương:
97
97
98
98
```js
99
99
obj.method();
@@ -104,15 +104,15 @@ var method = obj.method;
104
104
method();
105
105
```
106
106
107
-
Binding methods helps ensure that the second snippet works the same way as the first one.
107
+
Các phương thức binding giúp đảm bảo rằng đoạn code thứ hai hoạt động giống như cách đầu tiên.
108
108
109
-
With React, typically you only need to bind the methods you *pass* to other components. For example,`<button onClick={this.handleClick}>`passes`this.handleClick`so you want to bind it. However, it is unnecessary to bind the`render`method or the lifecycle methods: we don't pass them to other components.
109
+
Với React, thông thường bạn chỉ cần bind các phương thức bạn *truyền* cho các component khác. Ví dụ:`<button onClick = {this.handleClick}>`truyền`this.handleClick`vì vậy bạn phải bind nó. Tuy nhiên, không cần thiết phải bind hàm`render`hoặc phương thức vòng đời: chúng tôi không truyền chúng cho các component khác.
110
110
111
-
[This post by Yehuda Katz](https://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/)explains what binding is, and how functions work in JavaScript, in detail.
111
+
[Bài đăng này của Yehuda Katz](https://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/)giải thích binding là gì và cách các function hoạt động trong JavaScript, một cách chi tiết.
112
112
113
-
### Why is my function being called every time the component renders? {#why-is-my-function-being-called-every-time-the-component-renders}
113
+
### Tại sao function của tôi lại gọi lại mỗi khi component render? {#why-is-my-function-being-called-every-time-the-component-renders}
114
114
115
-
Make sure you aren't _calling the function_when you pass it to the component:
115
+
Hãy chắc chắn rằng bạn đã _call function_khi bạn truyền nó cho component:
116
116
117
117
```jsx
118
118
render() {
@@ -121,7 +121,7 @@ render() {
121
121
}
122
122
```
123
123
124
-
Instead, *pass the function itself* (without parens):
124
+
Thay thế, *pass the function itself* (without parens):
125
125
126
126
```jsx
127
127
render() {
@@ -130,9 +130,9 @@ render() {
130
130
}
131
131
```
132
132
133
-
### How do I pass a parameter to an event handler or callback? {#how-do-i-pass-a-parameter-to-an-event-handler-or-callback}
133
+
### Làm cách nào để truyền tham số cho xử lý sự kiện hoặc callback? {#how-do-i-pass-a-parameter-to-an-event-handler-or-callback}
134
134
135
-
You can use an arrow function to wrap around an event handler and pass parameters:
135
+
Bạn có thể sử dụng arrow function để bọc xung quanh xử lý sự kiện và truyền tham số:
136
136
137
137
```jsx
138
138
<button onClick={() =>this.handleClick(id)} />
@@ -144,7 +144,7 @@ This is equivalent to calling `.bind`:
#### Example: Passing params using arrow functions {#example-passing-params-using-arrow-functions}
147
+
#### Ví dụ: Truyền tham số sử dụng arrow function {#example-passing-params-using-arrow-functions}
148
148
149
149
```jsx
150
150
constA=65// ASCII character code
@@ -178,9 +178,9 @@ class Alphabet extends React.Component {
178
178
}
179
179
```
180
180
181
-
#### Example: Passing params using data-attributes {#example-passing-params-using-data-attributes}
181
+
#### Ví dụ: Truyền tham số sử dụng data-attributes {#example-passing-params-using-data-attributes}
182
182
183
-
Alternately, you can use DOM APIs to store data needed for event handlers. Consider this approach if you need to optimize a large number of elements or have a render tree that relies on React.PureComponent equality checks.
183
+
Thông thường, bạn có thể sử dụng DOM APIs để lưu trữ dữ liệu cần thiết cho xử lý sự kiện. Hãy xem xét phương pháp này nếu bạn cần tối ưu hóa một số lượng lớn các phần tử hoặc có một render tree dựa trên các phương thức kiểm tra sự bằng nhau React.PureComponent.
184
184
185
185
```jsx
186
186
constA=65// ASCII character code
@@ -218,23 +218,23 @@ class Alphabet extends React.Component {
218
218
}
219
219
```
220
220
221
-
### How can I prevent a function from being called too quickly or too many times in a row? {#how-can-i-prevent-a-function-from-being-called-too-quickly-or-too-many-times-in-a-row}
221
+
### Làm thế nào tôi có thể ngăn chặn một function được gọi quá nhanh hoặc quá nhiều lần liên tiếp? {#how-can-i-prevent-a-function-from-being-called-too-quickly-or-too-many-times-in-a-row}
222
222
223
-
If you have an event handler such as `onClick`or`onScroll`and want to prevent the callback from being fired too quickly, then you can limit the rate at which callback is executed. This can be done by using:
223
+
Nếu bạn có một sự kiện như `onClick`hoặc`onScroll`và muốn ngăn hàm callback gọi lại quá nhanh, thì bạn có thể giới hạn tốc độ thực hiện hàm callback. Điều này có thể được thực hiện bằng cách sử dụng:
224
224
225
-
-**throttling**: sample changes based on a time based frequency (eg [`_.throttle`](https://lodash.com/docs#throttle))
226
-
-**debouncing**: publish changes after a period of inactivity (eg [`_.debounce`](https://lodash.com/docs#debounce))
227
-
-**`requestAnimationFrame` throttling**: sample changes based on[`requestAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame) (eg [`raf-schd`](https://github.com/alexreardon/raf-schd))
225
+
-**throttling**: thay đổi dựa vào tần suất dựa trên thời gian (eg [`_.throttle`](https://lodash.com/docs#throttle))
226
+
-**debouncing**: thực hiện dựa vào những thay đổi sau một khoảng thời gian (eg [`_.debounce`](https://lodash.com/docs#debounce))
227
+
-**`requestAnimationFrame` throttling**: thay đổi mẫu dựa trên[`requestAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame) (eg [`raf-schd`](https://github.com/alexreardon/raf-schd))
228
228
229
-
See [this visualization](http://demo.nimius.net/debounce_throttle/)for a comparison of `throttle`and`debounce` functions.
229
+
Xem [mô phỏng này](http://demo.nimius.net/debounce_throttle/)để so sánh giữa các hàm `throttle`và`debounce`.
230
230
231
-
> Note:
231
+
> Lưu ý:
232
232
>
233
-
> `_.debounce`, `_.throttle`and`raf-schd`provide a `cancel`method to cancel delayed callbacks. You should either call this method from`componentWillUnmount`_or_ check to ensure that the component is still mounted within the delayed function.
233
+
> `_.debounce`, `_.throttle`và`raf-schd`cung cấp một phương thức `cancel`để huỷ các hàm callback đang bị trì hoãn. Bạn nên gọi phương thức này từ`componentWillUnmount`_hoặc_ kiểm tra để đảm bảo rằng component đó vẫn được gắn trong function bị trì hoãn.
234
234
235
235
#### Throttle {#throttle}
236
236
237
-
Throttling prevents a function from being called more than once in a given window of time. The example below throttles a "click" handler to prevent calling it more than once per second.
237
+
Throttle ngăn chặn một function được gọi nhiều lần trong một khung thời gian nhất định. Ví dụ dưới đây điều chỉnh một sự kiện xử lý "click" để ngăn chặn việc gọi nó nhiều hơn một lần mỗi giây.
238
238
239
239
```jsx
240
240
importthrottlefrom'lodash.throttle';
@@ -262,7 +262,7 @@ class LoadMoreButton extends React.Component {
262
262
263
263
#### Debounce {#debounce}
264
264
265
-
Debouncing ensures that a function will not be executed until after a certain amount of time has passed since it was last called. This can be useful when you have to perform some expensive calculation in response to an event that might dispatch rapidly (eg scroll or keyboard events). The example below debounces text input with a 250ms delay.
265
+
Debounce đảm bảo rằng một hàm sẽ không được thực thi sau một khoảng thời gian nhất định kể từ khi nó được gọi lần cuối. Điều này có thể hữu ích khi bạn phải thực hiện một số tính toán phức tạp để đáp ứng với một sự kiện có thể gửi đi nhanh chóng (ví dụ: các sự kiện scroll hoặc bàn phím). Ví dụ dưới đây nhập văn bản với độ trễ 250ms.
266
266
267
267
```jsx
268
268
importdebouncefrom'lodash.debounce';
@@ -304,11 +304,11 @@ class Searchbox extends React.Component {
[`requestAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame)is a way of queuing a function to be executed in the browser at the optimal time for rendering performance. A function that is queued with `requestAnimationFrame`will fire in the next frame. The browser will work hard to ensure that there are 60 frames per second (60 fps). However, if the browser is unable to it will naturally *limit* the amount of frames in a second. For example, a device might only be able to handle 30 fps and so you will only get 30 frames in that second. Using `requestAnimationFrame`for throttling is a useful technique in that it prevents you from doing more than 60 updates in a second. If you are doing 100 updates in a second this creates additional work for the browser that the user will not see anyway.
307
+
[`requestAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame) à cách sắp xếp thứ tự thực thi của một hàm trên trình duyệt tại thời điểm tối ưu cho hiệu suất hiển thị. Một hàm được xếp trong `requestAnimationFrame` sẽ kích hoạt trong khung tiếp theo. Trình duyệt sẽ làm việc liên tục để đảm bảo có 60 khung hình mỗi giây (60 fps). Tuy nhiên, nếu trình duyệt không thể, nó sẽ tự nhiên *giới hạn* số lượng khung hình trong một giây. Ví dụ: một thiết bị có thể chỉ có thể xử lý 30 fps và do đó bạn sẽ chỉ nhận được 30 khung hình trong giây đó. Sử dụng `requestAnimationFrame` để điều chỉnh là một kỹ thuật hữu ích ở chỗ nó ngăn bạn thực hiện hơn 60 cập nhật trong một giây. Nếu bạn đang thực hiện 100 bản cập nhật trong một giây, nó sẽ tạo ra thêm việc cho trình duyệt mà người dùng không hề nhìn thấy.
308
308
309
-
>**Note:**
309
+
>**Lưu ý:**
310
310
>
311
-
>Using this technique will only capture the last published value in a frame. You can see an example of how this optimization works on[`MDN`](https://developer.mozilla.org/en-US/docs/Web/Events/scroll)
311
+
>Sử dụng kĩ thuật này sẽ xem được giá trị cuối cùng của mỗi khung hình. Bạn có thể xem một ví dụ về cách tối ưu hóa này hoạt động trên[`MDN`](https://developer.mozilla.org/en-US/docs/Web/Events/scroll)
312
312
313
313
```jsx
314
314
importrafSchedulefrom'raf-schd';
@@ -349,6 +349,6 @@ class ScrollListener extends React.Component {
349
349
}
350
350
```
351
351
352
-
#### Testing your rate limiting {#testing-your-rate-limiting}
352
+
#### Kiểm tra tỉ lệ giới hạn {#testing-your-rate-limiting}
353
353
354
-
When testing your rate limiting code works correctly it is helpful to have the ability to fast forward time. If you are using [`jest`](https://facebook.github.io/jest/)then you can use [`mock timers`](https://facebook.github.io/jest/docs/en/timer-mocks.html)to fast forward time. If you are using `requestAnimationFrame`throttling then you may find[`raf-stub`](https://github.com/alexreardon/raf-stub)to be a useful tool to control the ticking of animation frames.
354
+
Khi kiểm tra code của bạn hoạt động có đang chính xác, thật hữu ích để có khả năng chuyển nhanh thời gian. Nếu bạn đang sử dụng [`jest`](https://facebook.github.io/jest/)thì bạn có thể sử dụng [`mock timers`](https://facebook.github.io/jest/docs/en/timer-mocks.html)để chuyển nhanh thời gian. Nếu bạn đang sử dụng `requestAnimationFrame`thì bạn có thể thấy[`raf-stub`](https://github.com/alexreardon/raf-stub)là một công cụ hữu ích để kiểm soát việc đánh dấu các khung hình động.
Copy file name to clipboardExpand all lines: content/docs/faq-styling.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ layout: docs
6
6
category: FAQ
7
7
---
8
8
9
-
### Làm thế nào tôi chèn class CSS cho components? {#how-do-i-add-css-classes-to-components}
9
+
### Làm thế nào tôi chèn class CSS cho các component? {#how-do-i-add-css-classes-to-components}
10
10
11
11
Truyền tên của class cho prop là `className`:
12
12
@@ -16,7 +16,7 @@ render() {
16
16
}
17
17
```
18
18
19
-
It is common for CSS classes to depend on the component props or state:
19
+
Thông thường các class CSS phụ thuộc vào các component props hoặc state:
20
20
21
21
```jsx
22
22
render() {
@@ -30,21 +30,21 @@ render() {
30
30
31
31
>Tip
32
32
>
33
-
>If you often find yourself writing code like this, [classnames](https://www.npmjs.com/package/classnames#usage-with-reactjs)package can simplify it.
33
+
>Nếu bạn thường thấy mình viết code như thế , [classnames](https://www.npmjs.com/package/classnames#usage-with-reactjs)thư viện này sẽ làm đơn giản nó.
34
34
35
35
### Tôi có dùng được inline styles? {#can-i-use-inline-styles}
36
36
37
37
Có, xem tài liệu styling tại [đây](/docs/dom-elements.html#style).
Các class CSS thường sẽ có hiệu xuất tốt hơn là inline styles.
41
+
Các class CSS thường sẽ có hiệu suất tốt hơn là inline styles.
42
42
43
43
### CSS-in-JS là gì? {#what-is-css-in-js}
44
44
45
-
"CSS-in-JS" refers to a pattern where CSS is composed using JavaScript instead of defined in external files. Read a comparison of CSS-in-JS libraries [here](https://github.com/MicheleBertoli/css-in-js).
45
+
"CSS-in-JS" đề cập đến một pattern trong đó CSS được tạo bằng JavaScript thay vì được định nghĩa trong các tệp bên ngoài. Đọc một bài viết so sánh các thư viện CSS-in-JS [ở đây](https://github.com/MicheleBertoli/css-in-js).
46
46
47
-
_Note that this functionality is not a part of React, but provided by third-party libraries._ React does not have an opinion about how styles are defined; if in doubt, a good starting point is to define your styles in a separate `*.css`file as usual and refer to them using[`className`](/docs/dom-elements.html#classname).
47
+
_Lưu ý đây không phải là thư viện của React, nhưng được cung câp bởi thư viện thứ ba._ React không có ý kiến về cách xác định style; nếu nghi ngờ, bạn có thể định nghĩa style trong một tệp `*.css`riêng và tham chiếu tới chúng sử dụng[`className`](/docs/dom-elements.html#classname).
48
48
49
49
### Tôi có thể dùng animations trong React? {#can-i-do-animations-in-react}
0 commit comments