diff --git a/src/content/learn/responding-to-events.md b/src/content/learn/responding-to-events.md index 782b6c0f1..6a34d05bd 100644 --- a/src/content/learn/responding-to-events.md +++ b/src/content/learn/responding-to-events.md @@ -1,24 +1,24 @@ --- -title: Responding to Events +title: Phản hồi các sự kiện --- -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. +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ự. -* Different ways to write an event handler -* How to pass event handling logic from a parent component -* How events propagate and how to stop them +* Những cách khác nhau để viết một hàm xử lý sự kiện +* Cách truyền logic xử lý sự kiện từ một component cha +* Cách các sự kiện lan truyền và cách dừng sự lan truyền sự kiện -## Adding event handlers {/*adding-event-handlers*/} +## Thêm các hàm xử lý sự kiện {/*adding-event-handlers*/} -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: +Để 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ì: @@ -34,11 +34,11 @@ export default function Button() { -You can make it show a message when a user clicks by following these three steps: +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: -1. Declare a function called `handleClick` *inside* your `Button` component. -2. Implement the logic inside that function (use `alert` to show the message). -3. Add `onClick={handleClick}` to the ` ); } function UploadButton() { return ( - ); } @@ -220,22 +220,22 @@ button { margin-right: 10px; } -Here, the `Toolbar` component renders a `PlayButton` and an `UploadButton`: +Tại đây, component `Toolbar` render một `PlayButton` và một `UploadButton`: -- `PlayButton` passes `handlePlayClick` as the `onClick` prop to the `Button` inside. -- `UploadButton` passes `() => alert('Uploading!')` as the `onClick` prop to the `Button` inside. +- `PlayButton` truyền `handlePlayClick` như một prop `onClick` tới `Button` bên trong. +- `UploadButton` truyền `() => alert('Uploading!')` như một prop `onClick` tới `Button` bên trong. -Finally, your `Button` component accepts a prop called `onClick`. It passes that prop directly to the built-in browser ` - ); @@ -268,9 +268,9 @@ button { margin-right: 10px; } -In this example, ` ); @@ -312,13 +312,13 @@ button { margin-right: 10px; } -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. +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 prop 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 prop 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. -## Event propagation {/*event-propagation*/} +## Sự lan truyền sự kiện {/*event-propagation*/} -Event handlers will also catch events from any children your component might have. We say that an event "bubbles" or "propagates" up the tree: it starts with where the event happened, and then goes up the tree. +Các hàm xử lý sự kiện cũng sẽ bắt các sự kiện từ bất cứ component con nào mà component của bạn có thể có. Ta nói sự kiện "nổi bọt" hay "lan truyền" lên cây component: bắt đầu từ nơi sự kiện xảy ra, và sau đó lan lên trên cây. -This `
` contains two buttons. Both the `
` *and* each button have their own `onClick` handlers. Which handlers do you think will fire when you click a button? +`
` này chứa hai nút. Cả `
` *và* mỗi nút đều có hàm xử lý `onClick` riêng. Bạn nghĩ hàm xử lý nào sẽ được kích hoạt khi bạn nhấn vào một nút? @@ -326,13 +326,13 @@ This `
` contains two buttons. Both the `
` *and* each button have their export default function Toolbar() { return (
{ - alert('You clicked on the toolbar!'); + alert('Bạn đã nhấn vào thanh công cụ!'); }}> - -
); @@ -349,19 +349,19 @@ button { margin: 5px; } -If you click on either button, its `onClick` will run first, followed by the parent `
`'s `onClick`. So two messages will appear. If you click the toolbar itself, only the parent `
`'s `onClick` will run. +Nếu bạn nhấn vào một trong hai nút, `onClick` của nút đó sẽ chạy trước, tiếp đến là `onClick` của `
` cha. Nên hai lời nhắn sẽ xuất hiện. Nếu bạn nhấn vào thanh công cụ, sẽ chỉ có `onClick` của `
` cha chạy. -All events propagate in React except `onScroll`, which only works on the JSX tag you attach it to. +Tất cả sự kiện đều lan truyền trong React ngoại trừ `onScroll`, nó chỉ hoạt động trên thẻ JSX mà bạn gắn nó. -### Stopping propagation {/*stopping-propagation*/} +### Dừng sự lan truyền {/*stopping-propagation*/} -Event handlers receive an **event object** as their only argument. By convention, it's usually called `e`, which stands for "event". You can use this object to read information about the event. +Các hàm xử lý sự kiện nhận một đối tượng sự kiện làm đối số duy nhất. Theo quy chuẩn, đối số này thường được gọi là `e`, viết tắt cho `event` (sự kiện). Bạn có thể sử dụng dối tượng này để đọc thông tin về sự kiện. -That event object also lets you stop the propagation. If you want to prevent an event from reaching parent components, you need to call `e.stopPropagation()` like this `Button` component does: +Đối tượng sự kiện đó cũng cho bạn dừng sự lan truyền. Nếu bạn muốn ngăn một sự kiện truyền tới các component cha, bạn cần gọi `e.stopPropagation()` như component `Button` dưới đây: @@ -380,13 +380,13 @@ function Button({ onClick, children }) { export default function Toolbar() { return (
{ - alert('You clicked on the toolbar!'); + alert('Bạn đã nhấn vào thanh công cụ!'); }}> - -
); @@ -403,43 +403,43 @@ button { margin: 5px; }
-When you click on a button: +Khi bạn nhấn vào một nút: -1. React calls the `onClick` handler passed to `
``` -Each event propagates in three phases: +Mỗi sự kiện lan truyền theo ba giai đoạn: -1. It travels down, calling all `onClickCapture` handlers. -2. It runs the clicked element's `onClick` handler. -3. It travels upwards, calling all `onClick` handlers. +1. Đi xuống, gọi tất cả hàm xử lý `onClickCapture`. +2. Chạy hàm xử lý `onClick` của element được nhấn. +3. Đi lên, gọi tất cả hàm xử lý `onClick`. -Capture events are useful for code like routers or analytics, but you probably won't use them in app code. +Việc bắt các sự kiện có lợi cho code như router hay phân tích, nhưng bạn có thể sẽ không cần sử dụng chúng trong code của ứng dụng. -### Passing handlers as alternative to propagation {/*passing-handlers-as-alternative-to-propagation*/} +### Truyền các hàm xử lý thay thế cho sự lan truyền {/*passing-handlers-as-alternative-to-propagation*/} -Notice how this click handler runs a line of code _and then_ calls the `onClick` prop passed by the parent: +Hãy để ý cách hàm xử lý `onClick` chạy một dòng code _và sau đó_ gọi prop `onClick` được truyền từ component cha: ```js {4,5} function Button({ onClick, children }) { @@ -454,13 +454,13 @@ function Button({ onClick, children }) { } ``` -You could add more code to this handler before calling the parent `onClick` event handler, too. This pattern provides an *alternative* to propagation. It lets the child component handle the event, while also letting the parent component specify some additional behavior. Unlike propagation, it's not automatic. But the benefit of this pattern is that you can clearly follow the whole chain of code that executes as a result of some event. +Bạn cũng có thể thêm code vào hàm xử lý trước khi gọi hàm xử lý sự kiện `onClick` cha. Pattern này cung cấp một *phương án thay thế* cho sự lan truyền. Nó cho component con xử lý sự kiện, đồng thời cũng cho component cha chỉ định thêm một số hành vi. Không như sự lan truyền, nó không hề tự động. Nhưng lợi ích của pattern này là bạn có thể theo dõi rõ ràng toàn bộ chuỗi code được thực thi do một số sự kiện gây ra. -If you rely on propagation and it's difficult to trace which handlers execute and why, try this approach instead. +Nếu bạn dựa vào sự lan truyền và thấy khó theo dõi hàm xử lý nào thực thi và tại sao, hãy thử phương pháp này xem. -### Preventing default behavior {/*preventing-default-behavior*/} +### Ngăn hành vi mặc định {/*preventing-default-behavior*/} -Some browser events have default behavior associated with them. For example, a `
` submit event, which happens when a button inside of it is clicked, will reload the whole page by default: +Một số sự kiện trình duyệt có hành vi mặc định gắn liền với chúng. Ví dụ, sự kiện submit của `` xảy ra khi một nút bên trong nó bị nhấn, sẽ mặc định tải lại toàn bộ trang: @@ -481,7 +481,7 @@ button { margin-left: 5px; } -You can call `e.preventDefault()` on the event object to stop this from happening: +Bạn có thể gọi `e.preventDefault()` trên đối tượng sự kiện để ngăn hành vi này xảy ra: @@ -505,38 +505,36 @@ button { margin-left: 5px; } -Don't confuse `e.stopPropagation()` and `e.preventDefault()`. They are both useful, but are unrelated: +Đừng nhầm lẫn `e.stopPropagation()` và `e.preventDefault()`. Cả hai đều có ích, nhưng không liên quan: -* [`e.stopPropagation()`](https://developer.mozilla.org/docs/Web/API/Event/stopPropagation) stops the event handlers attached to the tags above from firing. -* [`e.preventDefault()` ](https://developer.mozilla.org/docs/Web/API/Event/preventDefault) prevents the default browser behavior for the few events that have it. +* [`e.stopPropagation()`](https://developer.mozilla.org/docs/Web/API/Event/stopPropagation) không cho các hàm xử lý sự kiện được gắn vào các thẻ trên kích hoạt. +* [`e.preventDefault()` ](https://developer.mozilla.org/docs/Web/API/Event/preventDefault) ngăn các hành vi mặc định từ trình duyệt của một số ít các sự kiện. -## Can event handlers have side effects? {/*can-event-handlers-have-side-effects*/} +## Hàm xử lý sự kiện có thể có các side effects không? {/*can-event-handlers-have-side-effects*/} -Absolutely! Event handlers are the best place for side effects. +Chắc chắn rồi! Các hàm xử lý sự kiện là nơi dễ có side effects nhất. -Unlike rendering functions, event handlers don't need to be [pure](/learn/keeping-components-pure), so it's a great place to *change* something—for example, change an input's value in response to typing, or change a list in response to a button press. However, in order to change some information, you first need some way to store it. In React, this is done by using [state, a component's memory.](/learn/state-a-components-memory) You will learn all about it on the next page. +Không như các hàm rendering, các hàm xử lý sự kiện không cần phải [pure](/learn/keeping-components-pure), nên nó rất dễ *thay đổi* một thứ gì đó-ví dụ, thay đổi giá trị của input khi gõ phím, hay thay dổi một danh sách khi nhấn nút. Tuy nhiên, để thay đổi một số thông tin, trước tiên bạn cần một vài cách để chứa nó. Trong React, có thể làm điều này bằng cách sử dụng [state, bộ nhớ của component.](/learn/state-a-components-memory) Bạn sẽ học tất cả về nó trong trang tiếp theo. -* You can handle events by passing a function as a prop to an element like `