Skip to content

Commit c00ba73

Browse files
committed
Section: Adding event handlers
1 parent 5131f61 commit c00ba73

File tree

1 file changed

+66
-66
lines changed

1 file changed

+66
-66
lines changed

src/content/learn/responding-to-events.md

+66-66
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
---
2-
title: Responding to Events
2+
title: Phản hồi các sự kiện
33
---
44

55
<Intro>
66

7-
React lets you add *event handlers* to your JSX. Event handlers are your own functions that will be triggered in response to interactions like clicking, hovering, focusing form inputs, and so on.
7+
React cho bạn thêm các *hàm xử lý sự kiện* vào JSX. Hàm xử lý sự kiện là các hàm bạn tự định nghĩa mà sẽ được kích hoạt để phản hồi lại các tương tác như nhấn chuột, hover chuột hay focus các trường input của form, và các tương tác tương tự.
88

99
</Intro>
1010

1111
<YouWillLearn>
1212

13-
* Different ways to write an event handler
14-
* How to pass event handling logic from a parent component
15-
* How events propagate and how to stop them
13+
* Những cách khác nhau để viết một hàm xử lý sự kiện
14+
* Cách truyền logic xử lý sự kiện từ một component cha
15+
* Cách các sự kiện lan truyền và cách dừng sự lan truyền sự kiện
1616

1717
</YouWillLearn>
1818

19-
## Adding event handlers {/*adding-event-handlers*/}
19+
## Thêm các hàm xử lý sự kiện {/*adding-event-handlers*/}
2020

21-
To add an event handler, you will first define a function and then [pass it as a prop](/learn/passing-props-to-a-component) to the appropriate JSX tag. For example, here is a button that doesn't do anything yet:
21+
Để thêm các hàm xử lý sự kiện, bạn sẽ cần khai báo hàm rồi [truyền nó như một prop](/learn/passing-props-to-a-component) tới thẻ JSX thích hợp. Ví dụ, đây là một nút hiện tại chưa có chức năng gì:
2222

2323
<Sandpack>
2424

@@ -34,11 +34,11 @@ export default function Button() {
3434
3535
</Sandpack>
3636
37-
You can make it show a message when a user clicks by following these three steps:
37+
Bạn có thể làm nó hiển thị một lời nhắn khi người dùng nhấn vào qua các bước sau:
3838
39-
1. Declare a function called `handleClick` *inside* your `Button` component.
40-
2. Implement the logic inside that function (use `alert` to show the message).
41-
3. Add `onClick={handleClick}` to the `<button>` JSX.
39+
1. Khai báo một hàm `handleClick` *bên trong* component `Button` của bạn
40+
2. Thực thi logic bên trong hàm đó (sử dụng `alert` để hiện lời nhắn)
41+
3. Thêm `onClick={handleClick}` vào thẻ JSX `<button>`
4242
4343
<Sandpack>
4444
@@ -62,77 +62,77 @@ button { margin-right: 10px; }
6262
6363
</Sandpack>
6464
65-
You defined the `handleClick` function and then [passed it as a prop](/learn/passing-props-to-a-component) to `<button>`. `handleClick` is an **event handler.** Event handler functions:
65+
Bạn đã định nghĩa hàm `handleClick` rồi [truyền nó như một prop](/learn/passing-props-to-a-component) tới `<button>`. `handleClick` là một **hàm xử lý sự kiện**. Hàm xử lý sự kiện:
6666
67-
* Are usually defined *inside* your components.
68-
* Have names that start with `handle`, followed by the name of the event.
67+
* Thường được định nghĩa *bên trong* các component của bạn.
68+
* Có tên bắt đầu với `handle`, theo sau đó là tên sự kiện.
6969
70-
By convention, it is common to name event handlers as `handle` followed by the event name. You'll often see `onClick={handleClick}`, `onMouseEnter={handleMouseEnter}`, and so on.
70+
Theo quy chuẩn, ta thường đặt tên các hàm xử lý sự kiện là `handle` rồi đến tên sự kiện. Bạn sẽ hay thấy `onClick={handleClick}`, `onMouseEnter={handleMouseEnter}`, ...
7171
72-
Alternatively, you can define an event handler inline in the JSX:
72+
Cách khác, bạn có thể định nghĩa một hàm xử lý sự kiện theo kiểu inline trong JSX như sau:
7373
7474
```jsx
7575
<button onClick={function handleClick() {
7676
alert('You clicked me!');
7777
}}>
7878
```
7979
80-
Or, more concisely, using an arrow function:
80+
Hoặc, ngắn gọn hơn, sử dụng hàm mũi tên:
8181
8282
```jsx
8383
<button onClick={() => {
8484
alert('You clicked me!');
8585
}}>
8686
```
8787
88-
All of these styles are equivalent. Inline event handlers are convenient for short functions.
88+
Tất cả cách viết trên đều như nhau. Các hàm xử lý sự kiện inline sẽ tiện hơn cho các hàm ngắn.
8989
9090
<Pitfall>
9191
92-
Functions passed to event handlers must be passed, not called. For example:
92+
Các hàm phải được truyền cho hàm xử lý sự kiện chứ không được gọi. Ví dụ:
9393
94-
| passing a function (correct) | calling a function (incorrect) |
94+
| truyền hàm (đúng) | gọi hàm (sai) |
9595
| -------------------------------- | ---------------------------------- |
9696
| `<button onClick={handleClick}>` | `<button onClick={handleClick()}>` |
9797
98-
The difference is subtle. In the first example, the `handleClick` function is passed as an `onClick` event handler. This tells React to remember it and only call your function when the user clicks the button.
98+
Một sự khác biệt nhỏ. Trong ví dụ đầu tiên, hàm `handleClick` được truyền như một hàm xử lý sự kiện `onClick`. Điều này bảo React hãy nhớ hàm của bạn và chỉ được gọi nó khi người dùng nhấn nút.
9999
100-
In the second example, the `()` at the end of `handleClick()` fires the function *immediately* during [rendering](/learn/render-and-commit), without any clicks. This is because JavaScript inside the [JSX `{` and `}`](/learn/javascript-in-jsx-with-curly-braces) executes right away.
100+
Trong ví dụ thứ hai, cặp ngoặc `()` ở cuối `handleClick()` kích hoạt hàm *ngay lập tức* trong quá trình [rendering](/learn/render-and-commit) mà không cần nhấn nút. Đó là bởi vì Javascript trong [JSX `{` and `}`](/learn/javascript-in-jsx-with-curly-braces) được triển khai luôn.
101101
102-
When you write code inline, the same pitfall presents itself in a different way:
102+
Khi bạn viết code theo kiểu inline, những lưu ý tương tự được thể hiện theo một cách khác:
103103
104-
| passing a function (correct) | calling a function (incorrect) |
104+
| truyền hàm (đúng) | gọi hàm (sai) |
105105
| --------------------------------------- | --------------------------------- |
106106
| `<button onClick={() => alert('...')}>` | `<button onClick={alert('...')}>` |
107107
108108
109-
Passing inline code like this won't fire on click—it fires every time the component renders:
109+
Truyền code inline thế này sẽ không kích hoạt khi nhấn-nó kích hoạt mỗi lần component render:
110110
111111
```jsx
112-
// This alert fires when the component renders, not when clicked!
112+
// alert này sẽ chạy khi component render, không phải khi nút được nhấn!
113113
<button onClick={alert('You clicked me!')}>
114114
```
115115
116-
If you want to define your event handler inline, wrap it in an anonymous function like so:
116+
Nếu bạn muốn định nghĩa hàm xử lý sự kiện của mình kiểu inline, hãy bọc nó trong một hàm ẩn danh như thế này:
117117
118118
```jsx
119119
<button onClick={() => alert('You clicked me!')}>
120120
```
121121
122-
Rather than executing the code inside with every render, this creates a function to be called later.
122+
Thay vì triển khai code bên trong mỗi lần render, điều này tạo ra một hàm để được gọi sau này.
123123
124-
In both cases, what you want to pass is a function:
124+
Trong cả hai trường hợp, thứ bạn muốn truyền là một hàm:
125125
126-
* `<button onClick={handleClick}>` passes the `handleClick` function.
127-
* `<button onClick={() => alert('...')}>` passes the `() => alert('...')` function.
126+
* `<button onClick={handleClick}>` truyền hàm `handleClick`.
127+
* `<button onClick={() => alert('...')}>` truyền hàm `() => alert('...')`.
128128
129-
[Read more about arrow functions.](https://javascript.info/arrow-functions-basics)
129+
[Đọc thêm về hàm mũi tên.](https://javascript.info/arrow-functions-basics)
130130
131131
</Pitfall>
132132
133-
### Reading props in event handlers {/*reading-props-in-event-handlers*/}
133+
### Đọc props trong các hàm xử lý sự kiện {/*reading-props-in-event-handlers*/}
134134
135-
Because event handlers are declared inside of a component, they have access to the component's props. Here is a button that, when clicked, shows an alert with its `message` prop:
135+
Vì các hàm xử lý sự kiện được khai báo trong một component, chúng có quyền truy cập vào các props của component. Đây là một nút mà khi nhấn, hiện ra một alert với prop `message` của nó:
136136
137137
<Sandpack>
138138
@@ -148,11 +148,11 @@ function AlertButton({ message, children }) {
148148
export default function Toolbar() {
149149
return (
150150
<div>
151-
<AlertButton message="Playing!">
152-
Play Movie
151+
<AlertButton message="Đang phát!">
152+
Phát phim
153153
</AlertButton>
154-
<AlertButton message="Uploading!">
155-
Upload Image
154+
<AlertButton message="Đang tải!">
155+
Tải ảnh lên
156156
</AlertButton>
157157
</div>
158158
);
@@ -165,13 +165,13 @@ button { margin-right: 10px; }
165165
166166
</Sandpack>
167167
168-
This lets these two buttons show different messages. Try changing the messages passed to them.
168+
Điều này sẽ cho hai nút hiển thị hai lời nhắn khác nhau. Hãy thử thay đổi lời nhắn (`message`) được truyền cho các nút.
169169
170-
### Passing event handlers as props {/*passing-event-handlers-as-props*/}
170+
### Truyền hàm xử lý sự kiện như props {/*passing-event-handlers-as-props*/}
171171
172-
Often you'll want the parent component to specify a child's event handler. Consider buttons: depending on where you're using a `Button` component, you might want to execute a different function—perhaps one plays a movie and another uploads an image.
172+
Thông thường, bạn sẽ muốn component cha chỉ định hàm xử lý sự kiện của component con. Xem xét các nút: tuỳ thuộc vào nơi bạn đang sử dụng component `Button`, bạn có thể muốn thực thi một hàm khác-có thể là một nút phát phim còn một nút khác tải ảnh lên.
173173
174-
To do this, pass a prop the component receives from its parent as the event handler like so:
174+
Để làm được điều này, truyền một prop component nhận từ cha của nó như một hàm xử lý sự kiện:
175175
176176
<Sandpack>
177177
@@ -186,20 +186,20 @@ function Button({ onClick, children }) {
186186
187187
function PlayButton({ movieName }) {
188188
function handlePlayClick() {
189-
alert(`Playing ${movieName}!`);
189+
alert(`Đang phát ${movieName}!`);
190190
}
191191
192192
return (
193193
<Button onClick={handlePlayClick}>
194-
Play "{movieName}"
194+
Phát "{movieName}"
195195
</Button>
196196
);
197197
}
198198
199199
function UploadButton() {
200200
return (
201-
<Button onClick={() => alert('Uploading!')}>
202-
Upload Image
201+
<Button onClick={() => alert('Đang tải!')}>
202+
Tải ảnh lên
203203
</Button>
204204
);
205205
}
@@ -220,22 +220,22 @@ button { margin-right: 10px; }
220220
221221
</Sandpack>
222222
223-
Here, the `Toolbar` component renders a `PlayButton` and an `UploadButton`:
223+
Tại đây, component `Toolbar` render một `PlayButton` và một `UploadButton`:
224224
225-
- `PlayButton` passes `handlePlayClick` as the `onClick` prop to the `Button` inside.
226-
- `UploadButton` passes `() => alert('Uploading!')` as the `onClick` prop to the `Button` inside.
225+
- `PlayButton` truyền `handlePlayClick` như một prop `onClick` tới `Button` bên trong.
226+
- `UploadButton` truyền `() => alert('Uploading!')` như một prop `onClick` tới `Button` bên trong.
227227
228-
Finally, your `Button` component accepts a prop called `onClick`. It passes that prop directly to the built-in browser `<button>` with `onClick={onClick}`. This tells React to call the passed function on click.
228+
Cuối cùng, component `Button` của bạn nhận một prop được gọi là `onClick`. Nó truyền prop đó trực tiếp tới thẻ `<button>` có sẵn của trình duyệt với `onClick={onClick}`. Điều này bảo React gọi hàm được truyền khi nhấn nút.
229229
230-
If you use a [design system](https://uxdesign.cc/everything-you-need-to-know-about-design-systems-54b109851969), it's common for components like buttons to contain styling but not specify behavior. Instead, components like `PlayButton` and `UploadButton` will pass event handlers down.
230+
Nếu bạn sử dụng một [hệ thiết kế](https://uxdesign.cc/everything-you-need-to-know-about-design-systems-54b109851969), thông thường các component như các nút (`Button`) chứa styling chứ không chỉ định hành vi. Thay vào đó, các component như `PlayButton` `UploadButton` sẽ truyền hàm xử lý sự kiện xuống `Button`.
231231
232-
### Naming event handler props {/*naming-event-handler-props*/}
232+
### Đặt tên cho các props hàm xử lý sự kiện {/*naming-event-handler-props*/}
233233
234-
Built-in components like `<button>` and `<div>` only support [browser event names](/reference/react-dom/components/common#common-props) like `onClick`. However, when you're building your own components, you can name their event handler props any way that you like.
234+
Các component có sẵn như `<button>` `<div>` chỉ hỗ trợ các [tên sự kiện của trình duyệt](/reference/react-dom/components/common#common-props) như `onClick`. Tuy nhiên, khi xây dựng những component của riêng mình, bạn có thể đặt tên cho các props hàm xử lý sự kiện của các component đó tuỳ ý.
235235
236-
By convention, event handler props should start with `on`, followed by a capital letter.
236+
Theo quy chuẩn, các props hàm xử lý sự kiện nên bắt đầu bằng `on`, theo sau đó là chữ cái viết hoa.
237237
238-
For example, the `Button` component's `onClick` prop could have been called `onSmash`:
238+
Ví dụ, prop `onClick` của component `Button` có thể được gọi là `onSmash`:
239239
240240
<Sandpack>
241241
@@ -251,11 +251,11 @@ function Button({ onSmash, children }) {
251251
export default function App() {
252252
return (
253253
<div>
254-
<Button onSmash={() => alert('Playing!')}>
255-
Play Movie
254+
<Button onSmash={() => alert('Đang phát!')}>
255+
Phát phim
256256
</Button>
257-
<Button onSmash={() => alert('Uploading!')}>
258-
Upload Image
257+
<Button onSmash={() => alert('Đang tải!')}>
258+
Tải ảnh lên
259259
</Button>
260260
</div>
261261
);
@@ -268,18 +268,18 @@ button { margin-right: 10px; }
268268
269269
</Sandpack>
270270
271-
In this example, `<button onClick={onSmash}>` shows that the browser `<button>` (lowercase) still needs a prop called `onClick`, but the prop name received by your custom `Button` component is up to you!
271+
Ở ví dụ này, `<button onClick={onSmash}>` cho ta thấy `<button>` (viết thường) của trình duyệt vẫn cần một prop gọi là `onClick`, nhưng tên prop nhận bởi component tuỳ chỉnh `Button` là do bạn quyết định!
272272
273-
When your component supports multiple interactions, you might name event handler props for app-specific concepts. For example, this `Toolbar` component receives `onPlayMovie` and `onUploadImage` event handlers:
273+
Khi component của bạn hỗ trợ nhiều tương tác, bạn có thể đặt props hàm xử lý sự kiện cho các khái niệm riêng của ứng dụng. Ví dụ, component `Toolbar` này nhận hàm xử lý sự kiện `onPlayMovie` `onUploadImage`:
274274
275275
<Sandpack>
276276
277277
```js
278278
export default function App() {
279279
return (
280280
<Toolbar
281-
onPlayMovie={() => alert('Playing!')}
282-
onUploadImage={() => alert('Uploading!')}
281+
onPlayMovie={() => alert('Đang phát!')}
282+
onUploadImage={() => alert('Đang tải!')}
283283
/>
284284
);
285285
}
@@ -288,10 +288,10 @@ function Toolbar({ onPlayMovie, onUploadImage }) {
288288
return (
289289
<div>
290290
<Button onClick={onPlayMovie}>
291-
Play Movie
291+
Phát phim
292292
</Button>
293293
<Button onClick={onUploadImage}>
294-
Upload Image
294+
Tải ảnh lên
295295
</Button>
296296
</div>
297297
);
@@ -312,7 +312,7 @@ button { margin-right: 10px; }
312312
313313
</Sandpack>
314314
315-
Notice how the `App` component does not need to know *what* `Toolbar` will do with `onPlayMovie` or `onUploadImage`. That's an implementation detail of the `Toolbar`. Here, `Toolbar` passes them down as `onClick` handlers to its `Button`s, but it could later also trigger them on a keyboard shortcut. Naming props after app-specific interactions like `onPlayMovie` gives you the flexibility to change how they're used later.
315+
Hãy để ý cách component `App` không cần biết `Toolbar` sẽ làm gì với `onPlayMovie` hoặc `onUploadImage`. Đó là chi tiết thực thi của riêng `Toolbar`. Ở đây, `Toolbar` truyền chúng bằng các props hàm xử lý `onClick` xuống các `Button` của `Toolbar`, nhưng `Toolbar` cũng có thể kích hoạt chúng sau trên phím tắt của bàn phím. Đặt tên props theo các tương tác riêng của ứng dụng như `onPlayMovie` cho bạn sự linh hoạt trong việc thay đổi cách sử dụng chúng sau này.
316316
317317
## Event propagation {/*event-propagation*/}
318318

0 commit comments

Comments
 (0)