Skip to content

Commit 011f5ea

Browse files
committed
hooks-reference | part 3
1 parent 2faeedd commit 011f5ea

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

content/docs/hooks-reference.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,41 +96,41 @@ const [state, setState] = useState(() => {
9696
});
9797
```
9898
99-
#### Bailing out of a state update {#bailing-out-of-a-state-update}
99+
#### Bỏ qua một cập nhật state {#bailing-out-of-a-state-update}
100100

101-
If you update a State Hook to the same value as the current state, React will bail out without rendering the children or firing effects. (React uses the [`Object.is` comparison algorithm](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is#Description).)
101+
Nếu bạn cập nhật một State Hook với một giá trị giống với state hiện tại, React sẽ bỏ qua việc render the children hoặc bắn effects. (React sử dụng [Thuật toán so sánh `Object.is` ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is#Description).)
102102

103-
Note that React may still need to render that specific component again before bailing out. That shouldn't be a concern because React won't unnecessarily go "deeper" into the tree. If you're doing expensive calculations while rendering, you can optimize them with `useMemo`.
103+
Lưu ý rằng React có thể cần render lại component đặc biệt trước khi bỏ qua. Bạn không cần quan tâm đến nó bởi React sẽ không "đi sâu" vào cây (tree) một cách không cần thiết. Nếu bạn thực hiện việc tính toán phức tạp khi render, bạn có thể tối ưu nó bằng `useMemo`.
104104

105105
### `useEffect` {#useeffect}
106106

107107
```js
108108
useEffect(didUpdate);
109109
```
110110

111-
Accepts a function that contains imperative, possibly effectful code.
111+
Xác định một hàm với code có thể có effect hoặc không (possibly effectful code).
112112

113-
Mutations, subscriptions, timers, logging, and other side effects are not allowed inside the main body of a function component (referred to as React's _render phase_). Doing so will lead to confusing bugs and inconsistencies in the UI.
113+
Mutations, subscriptions, timers, logging, và các side effects không được phép sử dụng bên trong body của function component (gọi là React's _render phase_). Nếu làm vậy có thể sẽ dẫn đến những lỗi kì lạ và không nhất quán trên giao diện.
114114

115-
Instead, use `useEffect`. The function passed to `useEffect` will run after the render is committed to the screen. Think of effects as an escape hatch from React's purely functional world into the imperative world.
115+
Thay vào đó, sử dụng `useEffect`. Hàm được gọi bởi `useEffect` sẽ chạy sau khi render hoàn thành (render is committed to the screen). Hãy coi các effects như là cách để biến các hàm thuần túy trở thành các hàm của React.
116116

117-
By default, effects run after every completed render, but you can choose to fire them [only when certain values have changed](#conditionally-firing-an-effect).
117+
Mặc định, effetcs sẽ chạy mỗi lần sau khi render hoàn tất, nhưng bạn có thể điều chỉnh nó [chỉ khi chắc chắn giá trị thay đổi](#conditionally-firing-an-effect).
118118

119-
#### Cleaning up an effect {#cleaning-up-an-effect}
119+
#### Loại bỏ một effect (Cleaning up an effect) {#cleaning-up-an-effect}
120120

121-
Often, effects create resources that need to be cleaned up before the component leaves the screen, such as a subscription or timer ID. To do this, the function passed to `useEffect` may return a clean-up function. For example, to create a subscription:
121+
Thông thường, effets tạo ra tài nguyên mà nó cần được loại bỏ trước khi component rời khỏi màn hình, ví dụ như là subscription hoặc timer ID. Để làm vậy, hàm sử dụng ở `useEffect` có thể trả về một hàm clean-up. Ví dụ, để tạo một subscription:
122122

123123
```js
124124
useEffect(() => {
125125
const subscription = props.source.subscribe();
126126
return () => {
127-
// Clean up the subscription
127+
// Loại bỏ subscription
128128
subscription.unsubscribe();
129129
};
130130
});
131131
```
132132

133-
The clean-up function runs before the component is removed from the UI to prevent memory leaks. Additionally, if a component renders multiple times (as they typically do), the **previous effect is cleaned up before executing the next effect**. In our example, this means a new subscription is created on every update. To avoid firing an effect on every update, refer to the next section.
133+
Hàm clean-up chạy trước khi component bị loại bỏ khỏi UI để tránh bị rò rỉ bộ nhớ (memory leaks). Ngoài ra, nếu compoment render nhiều lần (thường sẽ như thế), **effect trước đó sẽ bị loại bỏ trước khi effect mới thực thi**. Trong ví dụ trên, subscription mới sẽ được tạo mỗi lần cập nhật. Để tránh bị effect mỗi khi update, hãy xem phần kế tiếp.
134134

135135
#### Timing of effects {#timing-of-effects}
136136

0 commit comments

Comments
 (0)