Skip to content

Commit 3d66a8f

Browse files
authored
Merge pull request #17 from ntban15/translate/handling-events
Translate Handling Events
2 parents 9d74145 + c695dd5 commit 3d66a8f

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

content/docs/handling-events.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
---
22
id: handling-events
3-
title: Handling Events
3+
title: Việc Bắt các Sự kiện
44
permalink: docs/handling-events.html
55
prev: state-and-lifecycle.html
66
next: conditional-rendering.html
77
redirect_from:
88
- "docs/events-ko-KR.html"
99
---
1010

11-
Handling events with React elements is very similar to handling events on DOM elements. There are some syntactic differences:
11+
Việc bắt sự kiện của những element React rất giống với những element DOM. Có một số khác biệt về cú pháp như:
1212

13-
* React events are named using camelCase, rather than lowercase.
14-
* With JSX you pass a function as the event handler, rather than a string.
13+
* Những sự kiện của React được đặt tên theo dạng camelCase, thay vì lowercase.
14+
* Với JSX, bạn có thể sử dụng "hàm" (function) để bắt sự kiện thay vì phải truyền vào một chuỗi.
1515

16-
For example, the HTML:
16+
Ví dụ, đoạn HTML sau:
1717

1818
```html
1919
<button onclick="activateLasers()">
2020
Activate Lasers
2121
</button>
2222
```
2323

24-
is slightly different in React:
24+
sẽ có đôi chút khác biệt trong React:
2525

2626
```js{1}
2727
<button onClick={activateLasers}>
2828
Activate Lasers
2929
</button>
3030
```
3131

32-
Another difference is that you cannot return `false` to prevent default behavior in React. You must call `preventDefault` explicitly. For example, with plain HTML, to prevent the default link behavior of opening a new page, you can write:
32+
Một điểm khác biệt nữa trong React là bạn không thể trả về `false` để chặn những hành vi mặc định mà phải gọi `preventDefault` trực tiếp. Lấy ví dụ với đoạn HTML sau, để chặn hành vi mặc định của đường dẫn là mở trang mới, bạn có thể viết:
3333

3434
```html
3535
<a href="#" onclick="console.log('The link was clicked.'); return false">
3636
Click me
3737
</a>
3838
```
3939

40-
In React, this could instead be:
40+
Còn trong React, bạn có thể làm như thế này:
4141

4242
```js{2-5,8}
4343
function ActionLink() {
@@ -54,19 +54,19 @@ function ActionLink() {
5454
}
5555
```
5656

57-
Here, `e` is a synthetic event. React defines these synthetic events according to the [W3C spec](https://www.w3.org/TR/DOM-Level-3-Events/), so you don't need to worry about cross-browser compatibility. See the [`SyntheticEvent`](/docs/events.html) reference guide to learn more.
57+
Ở đây, `e` là một sự kiện ảo. React định nghĩa những sự kiện ảo này dựa trên [chuẩn W3C](https://www.w3.org/TR/DOM-Level-3-Events/), nên bạn không cần lo lắng về sự tương thích giữa những browser. Hãy tham khảo tài liệu về [`SyntheticEvent`](/docs/events.html) để tìm hiểu thêm.
5858

59-
When using React you should generally not need to call `addEventListener` to add listeners to a DOM element after it is created. Instead, just provide a listener when the element is initially rendered.
59+
Khi làm việc với React, bạn thường không phải gọi `addEventListener` để gắn listener cho element DOM khi nó được khởi tạo. Thay vào đó, bạn chỉ cần gắn listener ngay lần đầu element được render.
6060

61-
When you define a component using an [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes), a common pattern is for an event handler to be a method on the class. For example, this `Toggle` component renders a button that lets the user toggle between "ON" and "OFF" states:
61+
Khi bạn định nghĩa component bằng [class ES6](https://developer.mozilla.org/vi/docs/Web/JavaScript/Reference/Classes), một mẫu thiết kế phổ biến là sử dụng phương thức của class để bắt sự kiện. Ví dụ, component `Toggle` dưới đây render một chiếc nút để người dùng thay đổi giữa state “ON” và “OFF":
6262

6363
```js{6,7,10-14,18}
6464
class Toggle extends React.Component {
6565
constructor(props) {
6666
super(props);
6767
this.state = {isToggleOn: true};
6868
69-
// This binding is necessary to make `this` work in the callback
69+
// Phép "ràng buộc" (bind) này là cần thiết để `this` hoạt động trong callback
7070
this.handleClick = this.handleClick.bind(this);
7171
}
7272
@@ -93,16 +93,16 @@ ReactDOM.render(
9393

9494
[**Try it on CodePen**](https://codepen.io/gaearon/pen/xEmzGg?editors=0010)
9595

96-
You have to be careful about the meaning of `this` in JSX callbacks. In JavaScript, class methods are not [bound](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind) by default. If you forget to bind `this.handleClick` and pass it to `onClick`, `this` will be `undefined` when the function is actually called.
96+
Bạn phải cẩn thận về ý nghĩa của `this` trong những callback JSX. Trong JavaScript, những phương thức của class mặc định không bị [ràng buộc](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind). Nếu bạn quên ràng buộc `this.handleClick` và truyền nó vào `onClick`, `this` sẽ có giá trị là `undefined` khi phương thức này được thực thi.
9797

98-
This is not React-specific behavior; it is a part of [how functions work in JavaScript](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/). Generally, if you refer to a method without `()` after it, such as `onClick={this.handleClick}`, you should bind that method.
98+
Đây không phải là tính chất của React mà là một phần trong [cách những hàm JavaScript hoạt động](https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/). Thông thường, nếu bạn trỏ tới phương thức mà không có `()` theo sau như `onClick={this.handleClick}`, bạn nên ràng buộc phương thức đó.
9999

100-
If calling `bind` annoys you, there are two ways you can get around this. If you are using the experimental [public class fields syntax](https://babeljs.io/docs/plugins/transform-class-properties/), you can use class fields to correctly bind callbacks:
100+
Nếu bạn thấy việc gọi `bind` phiền phức thì có hai giải pháp. Trong trường hợp bạn đang sử dụng [cú pháp thuộc tính class public](https://babeljs.io/docs/plugins/transform-class-properties/) thử nghiệm, bạn có thể dùng những thuộc tính class để ràng buộc callback một cách chính xác:
101101

102102
```js{2-6}
103103
class LoggingButton extends React.Component {
104-
// This syntax ensures `this` is bound within handleClick.
105-
// Warning: this is *experimental* syntax.
104+
// Cú pháp này đảm bảo `this` được ràng buộc trong handleClick.
105+
// Lưu ý: đây là cú pháp *thử nghiệm*.
106106
handleClick = () => {
107107
console.log('this is:', this);
108108
}
@@ -117,9 +117,9 @@ class LoggingButton extends React.Component {
117117
}
118118
```
119119

120-
This syntax is enabled by default in [Create React App](https://github.com/facebookincubator/create-react-app).
120+
Cú pháp này được bật theo mặc định trong [Create React App](https://github.com/facebookincubator/create-react-app).
121121

122-
If you aren't using class fields syntax, you can use an [arrow function](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) in the callback:
122+
Nếu bạn không sử dụng cú pháp thuộc tính class, bạn có thể dùng "hàm rút gọn" [arrow function](https://developer.mozilla.org/vi/docs/Web/JavaScript/Reference/Functions/Arrow_functions) trong callback:
123123

124124
```js{7-9}
125125
class LoggingButton extends React.Component {
@@ -128,7 +128,7 @@ class LoggingButton extends React.Component {
128128
}
129129
130130
render() {
131-
// This syntax ensures `this` is bound within handleClick
131+
// Cú pháp này đảm bảo `this` được ràng buộc trong handleClick
132132
return (
133133
<button onClick={(e) => this.handleClick(e)}>
134134
Click me
@@ -138,17 +138,17 @@ class LoggingButton extends React.Component {
138138
}
139139
```
140140

141-
The problem with this syntax is that a different callback is created each time the `LoggingButton` renders. In most cases, this is fine. However, if this callback is passed as a prop to lower components, those components might do an extra re-rendering. We generally recommend binding in the constructor or using the class fields syntax, to avoid this sort of performance problem.
141+
Vấn đề với cú pháp này nằm ở chỗ một callback khác sẽ được khởi tạo mỗi khi `LogginButton` render. Điều này ổn với đa số trường hợp. Tuy nhiên, nếu callback này được truyền duới dạng prop xuống những component con, những component này sẽ bị render lại. Nói chung, chúng tôi khuyến khích việc ràng buộc ở constructor hoặc sử dụng cú pháp thuộc tính class để ngăn chặn vấn đề về hiệu suất này.
142142

143-
## Passing Arguments to Event Handlers {#passing-arguments-to-event-handlers}
143+
## Truyền Tham số vào Hàm Bắt Sự kiện {#passing-arguments-to-event-handlers}
144144

145-
Inside a loop it is common to want to pass an extra parameter to an event handler. For example, if `id` is the row ID, either of the following would work:
145+
Trong một vòng lặp, việc truyền thêm tham số vào hàm bắt sự kiện khá phổ biến. Nếu `id` là số định danh của một hàng, thì hai ví dụ duới đây đều hoạt động được:
146146

147147
```js
148148
<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
149149
<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>
150150
```
151151

152-
The above two lines are equivalent, and use [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) and [`Function.prototype.bind`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind) respectively.
152+
Hai dòng code trên là tương đương, và lần lượt sử dụng [hàm rút gọn](https://developer.mozilla.org/vi/docs/Web/JavaScript/Reference/Functions/Arrow_functions) [`Function.prototype.bind`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind).
153153

154-
In both cases, the `e` argument representing the React event will be passed as a second argument after the ID. With an arrow function, we have to pass it explicitly, but with `bind` any further arguments are automatically forwarded.
154+
Trong cả hai trường hợp, tham số `e`, đại diện cho sự kiện React, sẽ được truyền là tham số thứ hai sau số định danh. Với hàm rút gọn, chúng ta phải truyền nó trực tiếp, nhưng với `bind` thì những tham số còn lại sẽ tự động nối tiếp.

0 commit comments

Comments
 (0)