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: src/content/learn/responding-to-events.md
+66-66
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,24 @@
1
1
---
2
-
title: Responding to Events
2
+
title: Phản hồi các sự kiện
3
3
---
4
4
5
5
<Intro>
6
6
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ự.
8
8
9
9
</Intro>
10
10
11
11
<YouWillLearn>
12
12
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
## Thêm các hàm xử lý sự kiện {/*adding-event-handlers*/}
20
20
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ì:
22
22
23
23
<Sandpack>
24
24
@@ -34,11 +34,11 @@ export default function Button() {
34
34
35
35
</Sandpack>
36
36
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:
38
38
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>`
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:
66
66
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.
69
69
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}`, ...
71
71
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:
73
73
74
74
```jsx
75
75
<button onClick={function handleClick() {
76
76
alert('You clicked me!');
77
77
}}>
78
78
```
79
79
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:
81
81
82
82
```jsx
83
83
<button onClick={() => {
84
84
alert('You clicked me!');
85
85
}}>
86
86
```
87
87
88
-
All of these styles are equivalent. Inlineevent 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.
89
89
90
90
<Pitfall>
91
91
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ụ:
93
93
94
-
|passing a function (correct) | calling a function (incorrect) |
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.
99
99
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.
101
101
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:
103
103
104
-
| passing a function (correct) | calling a function (incorrect) |
* `<button onClick={handleClick}>` truyền hàm `handleClick`.
127
+
* `<button onClick={() => alert('...')}>` truyền hàm `() => alert('...')`.
128
128
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)
130
130
131
131
</Pitfall>
132
132
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*/}
134
134
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ó:
136
136
137
137
<Sandpack>
138
138
@@ -148,11 +148,11 @@ function AlertButton({ message, children }) {
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.
169
169
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*/}
171
171
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.
173
173
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 mà component nhận từ cha của nó như một hàm xử lý sự kiện:
175
175
176
176
<Sandpack>
177
177
@@ -186,20 +186,20 @@ function Button({ onClick, children }) {
Here, the`Toolbar`component renders a `PlayButton`and an`UploadButton`:
223
+
Tại đây, component `Toolbar` render một `PlayButton` và một `UploadButton`:
224
224
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.
227
227
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.
229
229
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` và `UploadButton` sẽ truyền hàm xử lý sự kiện xuống `Button`.
### Đặt tên cho các props hàm xử lý sự kiện {/*naming-event-handler-props*/}
233
233
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>` và `<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ỳ ý.
235
235
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.
237
237
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`:
239
239
240
240
<Sandpack>
241
241
@@ -251,11 +251,11 @@ function Button({ onSmash, children }) {
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!
272
272
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` và `onUploadImage`:
274
274
275
275
<Sandpack>
276
276
277
277
```js
278
278
export default function App() {
279
279
return (
280
280
<Toolbar
281
-
onPlayMovie={() =>alert('Playing!')}
282
-
onUploadImage={() =>alert('Uploading!')}
281
+
onPlayMovie={() => alert('Đang phát!')}
282
+
onUploadImage={() => alert('Đang tải!')}
283
283
/>
284
284
);
285
285
}
@@ -288,10 +288,10 @@ function Toolbar({ onPlayMovie, onUploadImage }) {
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.
0 commit comments