Skip to content

Commit 67ee62b

Browse files
authored
Merge pull request #361 from leduc1901/leduc1901/lazy
Translation: Lazy
2 parents 3b8b9ba + 20577f7 commit 67ee62b

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/content/reference/react/lazy.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: lazy
44

55
<Intro>
66

7-
`lazy` lets you defer loading component's code until it is rendered for the first time.
7+
`lazy` cho phép bạn trì hoãn việc tải code của component cho tới khi được render lần đầu tiên.
88

99
```js
1010
const SomeComponent = lazy(load)
@@ -16,27 +16,27 @@ const SomeComponent = lazy(load)
1616

1717
---
1818

19-
## Reference {/*reference*/}
19+
## Tham khảo {/*reference*/}
2020

2121
### `lazy(load)` {/*lazy*/}
2222

23-
Call `lazy` outside your components to declare a lazy-loaded React component:
23+
Gọi `lazy` bên ngoài các component của bạn để khai báo một React component được lazy-load:
2424

2525
```js
2626
import { lazy } from 'react';
2727

2828
const MarkdownPreview = lazy(() => import('./MarkdownPreview.js'));
2929
```
3030

31-
[See more examples below.](#usage)
31+
[Xem thêm ví dụ ở dưới.](#usage)
3232

33-
#### Parameters {/*parameters*/}
33+
#### Tham số {/*parameters*/}
3434

35-
* `load`: A function that returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or another *thenable* (a Promise-like object with a `then` method). React will not call `load` until the first time you attempt to render the returned component. After React first calls `load`, it will wait for it to resolve, and then render the resolved value as a React component. Both the returned Promise and the Promise's resolved value will be cached, so React will not call `load` more than once. If the Promise rejects, React will `throw` the rejection reason for the nearest Error Boundary to handle.
35+
* `load`: Một function trả về một [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) hoặc một *thenable* (một đối tượng giống Promise với một phương thức `then`). React sẽ không gọi hàm `load` cho tới khi bạn render component được trả về. Sau khi React gọi `load` lần đầu tiên, React sẽ đợi cho tới khi hàm này được giải quyết xong (resolve), sau đó sẽ render giá trị được giải quyết như một React component. Cả Promise được trả về và giá trị đã được giải quyết của Promise đó đều được lưu lại, nên React sẽ không gọi `load` thêm nữa. Nếu Promise từ chối, React sẽ `throw` lý do từ chối cho Error Boundary gần nhất để xử lý.
3636

37-
#### Returns {/*returns*/}
37+
#### Giá trị trả về {/*returns*/}
3838

39-
`lazy` returns a React component you can render in your tree. While the code for the lazy component is still loading, attempting to render it will *suspend.* Use [`<Suspense>`](/reference/react/Suspense) to display a loading indicator while it's loading.
39+
`lazy` trả về một React component mà bạn có thể render. Khi đoạn code cho lazy component đó đang được tải, việc thực hiện render nó sẽ *được hoãn lại.* Sử dụng [`<Suspense>`](/reference/react/Suspense) để hiển thị một chỉ báo đang tải (loading).
4040

4141
---
4242

@@ -52,27 +52,27 @@ You need to return a [Promise](https://developer.mozilla.org/en-US/docs/Web/Java
5252

5353
---
5454

55-
## Usage {/*usage*/}
55+
## Các sử dụng {/*usage*/}
5656

57-
### Lazy-loading components with Suspense {/*suspense-for-code-splitting*/}
57+
### Lazy-loading components với Suspense {/*suspense-for-code-splitting*/}
5858

59-
Usually, you import components with the static [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) declaration:
59+
Thông thường, bạn import các component bằng khai báo tĩnh [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import):
6060

6161
```js
6262
import MarkdownPreview from './MarkdownPreview.js';
6363
```
6464

65-
To defer loading this component's code until it's rendered for the first time, replace this import with:
65+
Để trì hoãn việc tải code của component cho tới khi nó được render lần đầu tiên, thay thế import với:
6666

6767
```js
6868
import { lazy } from 'react';
6969

7070
const MarkdownPreview = lazy(() => import('./MarkdownPreview.js'));
7171
```
7272

73-
This code relies on [dynamic `import()`,](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) which might require support from your bundler or framework.
73+
Đoạn code này phụ thuộc vào [dynamic `import()`,](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) có thể sẽ cần được hỗ trợ bởi bundler hoặc framework của bạn.
7474

75-
Now that your component's code loads on demand, you also need to specify what should be displayed while it is loading. You can do this by wrapping the lazy component or any of its parents into a [`<Suspense>`](/reference/react/Suspense) boundary:
75+
Bây giờ code của component sẽ tải khi được yêu cầu, bạn sẽ cần phải xác định cái gì sẽ được hiển thị khi component đang được tải. Bạn có thể làm điều này bằng việc bọc lazy component hoặc bất kì phần tử cha của nó vào trong [`<Suspense>`](/reference/react/Suspense):
7676

7777
```js {1,4}
7878
<Suspense fallback={<Loading />}>
@@ -81,7 +81,7 @@ Now that your component's code loads on demand, you also need to specify what sh
8181
</Suspense>
8282
```
8383

84-
In this example, the code for `MarkdownPreview` won't be loaded until you attempt to render it. If `MarkdownPreview` hasn't loaded yet, `Loading` will be shown in its place. Try ticking the checkbox:
84+
Trong ví dụ này, đoạn code cho `MarkdownPreview` sẽ không được tải cho tới khi bạn thực thi việc render nó. Nếu `MarkdownPreview` vẫn chưa được tải xong, `Loading` sẽ được hiển thị vào vị trí của nó. Hãy thử tích vào checkbox dưới đây:
8585

8686
<Sandpack>
8787

@@ -112,7 +112,7 @@ export default function MarkdownEditor() {
112112
);
113113
}
114114

115-
// Add a fixed delay so you can see the loading state
115+
// Thêm một khoảng thời gian trì hoãn để bạn có thể thấy được loading state
116116
function delayForDemo(promise) {
117117
return new Promise(resolve => {
118118
setTimeout(resolve, 2000);
@@ -175,34 +175,34 @@ body {
175175

176176
</Sandpack>
177177

178-
This demo loads with an artificial delay. The next time you untick and tick the checkbox, `Preview` will be cached, so there will be no loading state. To see the loading state again, click "Reset" on the sandbox.
178+
Đoạn demo này được tải với một khoảng thời gian trì hoãn cố ý. Lần sau khi bạn bỏ chọn và chọn lại, `Preview` sẽ được lưu lại, lên sẽ không còn loading state nữa. Để nhìn lại loading state, hãy bấm "Reset" trên sandbox.
179179

180-
[Learn more about managing loading states with Suspense.](/reference/react/Suspense)
180+
[Tìm hiểu thêm về quản lý các loading state với Suspense.](/reference/react/Suspense)
181181

182182
---
183183

184-
## Troubleshooting {/*troubleshooting*/}
184+
## Khắc phục sự cố {/*troubleshooting*/}
185185

186-
### My `lazy` component's state gets reset unexpectedly {/*my-lazy-components-state-gets-reset-unexpectedly*/}
186+
### State của `lazy` component của tôi bị đặt lại ngoài ý muốn {/*my-lazy-components-state-gets-reset-unexpectedly*/}
187187

188-
Do not declare `lazy` components *inside* other components:
188+
Không khai báo các `lazy` component *bên trong* các component khác:
189189

190190
```js {4-5}
191191
import { lazy } from 'react';
192192

193193
function Editor() {
194-
// 🔴 Bad: This will cause all state to be reset on re-renders
194+
// 🔴 Bad: Điều này sẽ khiến các state bị đặt lại khi render lại
195195
const MarkdownPreview = lazy(() => import('./MarkdownPreview.js'));
196196
// ...
197197
}
198198
```
199199

200-
Instead, always declare them at the top level of your module:
200+
Thay vào đó, luôn luôn khai báo chúng ở trên cùng module của bạn:
201201

202202
```js {3-4}
203203
import { lazy } from 'react';
204204

205-
// ✅ Good: Declare lazy components outside of your components
205+
// ✅ Good: Khai báo các lazy component bên ngoài các component
206206
const MarkdownPreview = lazy(() => import('./MarkdownPreview.js'));
207207

208208
function Editor() {

0 commit comments

Comments
 (0)