diff --git a/src/content/learn/synchronizing-with-effects.md b/src/content/learn/synchronizing-with-effects.md index 115075161..dc8df6a38 100644 --- a/src/content/learn/synchronizing-with-effects.md +++ b/src/content/learn/synchronizing-with-effects.md @@ -1,65 +1,65 @@ --- -title: 'Synchronizing with Effects' +title: 'Đồng bộ hóa với Effect' --- -Some components need to synchronize with external systems. For example, you might want to control a non-React component based on the React state, set up a server connection, or send an analytics log when a component appears on the screen. *Effects* let you run some code after rendering so that you can synchronize your component with some system outside of React. +Một số component cần đồng bộ hóa với các hệ thống bên ngoài. Ví dụ, bạn có thể muốn điều khiển một component không phải React dựa trên state của React, thiết lập kết nối server, hoặc gửi log phân tích khi một component xuất hiện trên màn hình. *Effect* cho phép bạn chạy một số code sau khi render để có thể đồng bộ hóa component của bạn với một số hệ thống bên ngoài React. -- What Effects are -- How Effects are different from events -- How to declare an Effect in your component -- How to skip re-running an Effect unnecessarily -- Why Effects run twice in development and how to fix them +- Effect là gì +- Effect khác với event như thế nào +- Cách khai báo một Effect trong component của bạn +- Cách bỏ qua việc chạy lại Effect một cách không cần thiết +- Tại sao Effect chạy hai lần trong quá trình phát triển và cách khắc phục chúng -## What are Effects and how are they different from events? {/*what-are-effects-and-how-are-they-different-from-events*/} +## Effect là gì và chúng khác với event như thế nào? {/*what-are-effects-and-how-are-they-different-from-events*/} -Before getting to Effects, you need to be familiar with two types of logic inside React components: +Trước khi đến với Effect, bạn cần làm quen với hai loại logic bên trong các component React: -- **Rendering code** (introduced in [Describing the UI](/learn/describing-the-ui)) lives at the top level of your component. This is where you take the props and state, transform them, and return the JSX you want to see on the screen. [Rendering code must be pure.](/learn/keeping-components-pure) Like a math formula, it should only _calculate_ the result, but not do anything else. +- **Code render** (được giới thiệu trong [Mô tả UI](/learn/describing-the-ui)) tồn tại ở cấp độ cao nhất của component của bạn. Đây là nơi bạn lấy props và state, chuyển đổi chúng, và trả về JSX mà bạn muốn thấy trên màn hình. [Code render phải thuần khiết.](/learn/keeping-components-pure) Giống như một công thức toán học, nó chỉ nên *tính toán* kết quả, nhưng không làm gì khác. -- **Event handlers** (introduced in [Adding Interactivity](/learn/adding-interactivity)) are nested functions inside your components that *do* things rather than just calculate them. An event handler might update an input field, submit an HTTP POST request to buy a product, or navigate the user to another screen. Event handlers contain ["side effects"](https://en.wikipedia.org/wiki/Side_effect_(computer_science)) (they change the program's state) caused by a specific user action (for example, a button click or typing). +- **Event handler** (được giới thiệu trong [Thêm tính tương tác](/learn/adding-interactivity)) là những function lồng nhau bên trong component của bạn mà *thực hiện* những việc thay vì chỉ tính toán chúng. Một event handler có thể cập nhật một trường input, gửi một HTTP POST request để mua một sản phẩm, hoặc điều hướng người dùng đến màn hình khác. Event handler chứa ["side effect"](https://en.wikipedia.org/wiki/Side_effect_(computer_science)) (chúng thay đổi state của chương trình) được gây ra bởi một hành động cụ thể của người dùng (ví dụ, click nút hoặc gõ phím). -Sometimes this isn't enough. Consider a `ChatRoom` component that must connect to the chat server whenever it's visible on the screen. Connecting to a server is not a pure calculation (it's a side effect) so it can't happen during rendering. However, there is no single particular event like a click that causes `ChatRoom` to be displayed. +Đôi khi điều này chưa đủ. Hãy xem xét một component `ChatRoom` mà phải kết nối với chat server mỗi khi nó hiển thị trên màn hình. Kết nối với server không phải là một phép tính thuần khiết (đó là một side effect) nên nó không thể xảy ra trong quá trình rendering. Tuy nhiên, không có một event cụ thể nào như click mà khiến `ChatRoom` được hiển thị. -***Effects* let you specify side effects that are caused by rendering itself, rather than by a particular event.** Sending a message in the chat is an *event* because it is directly caused by the user clicking a specific button. However, setting up a server connection is an *Effect* because it should happen no matter which interaction caused the component to appear. Effects run at the end of a [commit](/learn/render-and-commit) after the screen updates. This is a good time to synchronize the React components with some external system (like network or a third-party library). +***Effect* cho phép bạn chỉ định các side effect được gây ra bởi chính quá trình rendering, thay vì bởi một event cụ thể.** Gửi tin nhắn trong chat là một *event* vì nó được gây ra trực tiếp bởi người dùng click vào một nút cụ thể. Tuy nhiên, thiết lập kết nối server là một *Effect* vì nó nên xảy ra bất kể tương tác nào khiến component xuất hiện. Effect chạy ở cuối của một [commit](/learn/render-and-commit) sau khi màn hình cập nhật. Đây là thời điểm tốt để đồng bộ hóa các component React với một hệ thống bên ngoài (như mạng hoặc thư viện bên thứ ba). -Here and later in this text, capitalized "Effect" refers to the React-specific definition above, i.e. a side effect caused by rendering. To refer to the broader programming concept, we'll say "side effect". +Ở đây và sau này trong văn bản này, "Effect" viết hoa đề cập đến định nghĩa cụ thể của React ở trên, tức là một side effect được gây ra bởi rendering. Để đề cập đến khái niệm lập trình rộng hơn, chúng ta sẽ nói "side effect". -## You might not need an Effect {/*you-might-not-need-an-effect*/} +## Bạn có thể không cần Effect {/*you-might-not-need-an-effect*/} -**Don't rush to add Effects to your components.** Keep in mind that Effects are typically used to "step out" of your React code and synchronize with some *external* system. This includes browser APIs, third-party widgets, network, and so on. If your Effect only adjusts some state based on other state, [you might not need an Effect.](/learn/you-might-not-need-an-effect) +**Đừng vội vàng thêm Effect vào component của bạn.** Hãy nhớ rằng Effect thường được sử dụng để "thoát ra" khỏi code React của bạn và đồng bộ hóa với một hệ thống *bên ngoài* nào đó. Điều này bao gồm các API trình duyệt, widget bên thứ ba, mạng, v.v. Nếu Effect của bạn chỉ điều chỉnh một số state dựa trên state khác, [bạn có thể không cần Effect.](/learn/you-might-not-need-an-effect) -## How to write an Effect {/*how-to-write-an-effect*/} +## Cách viết một Effect {/*how-to-write-an-effect*/} -To write an Effect, follow these three steps: +Để viết một Effect, hãy làm theo ba bước sau: -1. **Declare an Effect.** By default, your Effect will run after every [commit](/learn/render-and-commit). -2. **Specify the Effect dependencies.** Most Effects should only re-run *when needed* rather than after every render. For example, a fade-in animation should only trigger when a component appears. Connecting and disconnecting to a chat room should only happen when the component appears and disappears, or when the chat room changes. You will learn how to control this by specifying *dependencies.* -3. **Add cleanup if needed.** Some Effects need to specify how to stop, undo, or clean up whatever they were doing. For example, "connect" needs "disconnect", "subscribe" needs "unsubscribe", and "fetch" needs either "cancel" or "ignore". You will learn how to do this by returning a *cleanup function*. +1. **Khai báo một Effect.** Theo mặc định, Effect của bạn sẽ chạy sau mỗi [commit](/learn/render-and-commit). +2. **Chỉ định các dependency của Effect.** Hầu hết các Effect chỉ nên chạy lại *khi cần thiết* thay vì sau mỗi lần render. Ví dụ, animation fade-in chỉ nên kích hoạt khi một component xuất hiện. Kết nối và ngắt kết nối với phòng chat chỉ nên xảy ra khi component xuất hiện và biến mất, hoặc khi phòng chat thay đổi. Bạn sẽ học cách điều khiển điều này bằng cách chỉ định *dependency.* +3. **Thêm cleanup nếu cần.** Một số Effect cần chỉ định cách dừng, hoàn tác, hoặc dọn dẹp bất cứ thứ gì chúng đang làm. Ví dụ, "connect" cần "disconnect", "subscribe" cần "unsubscribe", và "fetch" cần "cancel" hoặc "ignore". Bạn sẽ học cách thực hiện điều này bằng cách trả về một *cleanup function*. -Let's look at each of these steps in detail. +Hãy xem xét từng bước một cách chi tiết. -### Step 1: Declare an Effect {/*step-1-declare-an-effect*/} +### Bước 1: Khai báo một Effect {/*step-1-declare-an-effect*/} -To declare an Effect in your component, import the [`useEffect` Hook](/reference/react/useEffect) from React: +Để khai báo một Effect trong component của bạn, hãy import Hook [`useEffect`](/reference/react/useEffect) từ React: ```js import { useEffect } from 'react'; ``` -Then, call it at the top level of your component and put some code inside your Effect: +Sau đó, gọi nó ở cấp độ cao nhất của component và đặt một số code bên trong Effect của bạn: ```js {2-4} function MyComponent() { @@ -70,15 +70,15 @@ function MyComponent() { } ``` -Every time your component renders, React will update the screen *and then* run the code inside `useEffect`. In other words, **`useEffect` "delays" a piece of code from running until that render is reflected on the screen.** +Mỗi khi component của bạn render, React sẽ cập nhật màn hình *và sau đó* chạy code bên trong `useEffect`. Nói cách khác, **`useEffect` "trì hoãn" một đoạn code khỏi việc chạy cho đến khi lần render đó được phản ánh trên màn hình.** -Let's see how you can use an Effect to synchronize with an external system. Consider a `` React component. It would be nice to control whether it's playing or paused by passing an `isPlaying` prop to it: +Hãy xem cách bạn có thể sử dụng Effect để đồng bộ hóa với một hệ thống bên ngoài. Hãy xem xét một component React ``. Sẽ rất tuyệt nếu có thể điều khiển việc nó đang phát hay tạm dừng bằng cách truyền một prop `isPlaying` cho nó: ```js ; ``` -Your custom `VideoPlayer` component renders the built-in browser [`