Skip to content

Commit ac5e0ea

Browse files
committed
translate(useId): translate useId documentation to Vietnamese
1 parent b149357 commit ac5e0ea

File tree

1 file changed

+52
-51
lines changed

1 file changed

+52
-51
lines changed

src/content/reference/react/useId.md

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: useId
44

55
<Intro>
66

7-
`useId` is a React Hook for generating unique IDs that can be passed to accessibility attributes.
7+
`useId` là một React Hook dùng để sinh ra các ID duy nhất có thể được dùng để truyền vào các thuộc tính accessibility.
88

99
```js
1010
const id = useId()
@@ -20,7 +20,7 @@ const id = useId()
2020

2121
### `useId()` {/*useid*/}
2222

23-
Call `useId` at the top level of your component to generate a unique ID:
23+
Gọi `useId` top level của component để sinh ra một ID duy nhất:
2424

2525
```js
2626
import { useId } from 'react';
@@ -30,37 +30,37 @@ function PasswordField() {
3030
// ...
3131
```
3232
33-
[See more examples below.](#usage)
33+
[Xem nhiều ví dụ hơn ở bên dưới.](#usage)
3434
35-
#### Parameters {/*parameters*/}
35+
#### Tham số {/*parameters*/}
3636
37-
`useId` does not take any parameters.
37+
`useId` không nhận tham số.
3838
39-
#### Returns {/*returns*/}
39+
#### Trả về {/*returns*/}
4040
41-
`useId` returns a unique ID string associated with this particular `useId` call in this particular component.
41+
`useId` trả về một ID dạng chuỗi duy nhất với lần gọi `useId` trong component hiện tại.
4242
43-
#### Caveats {/*caveats*/}
43+
#### Lưu ý {/*caveats*/}
4444
45-
* `useId` is a Hook, so you can only call it **at the top level of your component** or your own Hooks. You can't call it inside loops or conditions. If you need that, extract a new component and move the state into it.
45+
* `useId` là một Hook, vì vậy bạn chỉ có thể gọi nó **ở top level của component** hoặc các Hook tuỳ chỉnh của bạn. Bạn không thể gọi nó bên trong vòng lặp hoặc trong câu lệnh điều kiện. Nếu bạn cần làm vậy, hãy tách nó thành một component mới và di chuyển state vào đó.
4646
47-
* `useId` **should not be used to generate keys** in a list. [Keys should be generated from your data.](/learn/rendering-lists#where-to-get-your-key)
47+
* `useId` **không nên được sử dụng để sinh key** cho danh sách. [Key nên được tạo từ dữ liệu của bạn.](/learn/rendering-lists#where-to-get-your-key)
4848
49-
* `useId` currently cannot be used in [async Server Components](/reference/rsc/server-components#async-components-with-server-components).
49+
* `useId` hiện tại không thể sử dụng trong [async Server Component](/reference/rsc/server-components#async-components-with-server-components).
5050
5151
---
5252
53-
## Usage {/*usage*/}
53+
## Sử dụng {/*usage*/}
5454
5555
<Pitfall>
5656
57-
**Do not call `useId` to generate keys in a list.** [Keys should be generated from your data.](/learn/rendering-lists#where-to-get-your-key)
57+
**Không gọi `useId` để sinh key trong một danh sách.** [Key nên được tạo từ dữ liệu của bạn.](/learn/rendering-lists#where-to-get-your-key)
5858
5959
</Pitfall>
6060
61-
### Generating unique IDs for accessibility attributes {/*generating-unique-ids-for-accessibility-attributes*/}
61+
### Sinh ID duy nhất cho các thuộc tính accessibility {/*generating-unique-ids-for-accessibility-attributes*/}
6262
63-
Call `useId` at the top level of your component to generate a unique ID:
63+
Gọi `useId` tại top level của component để sinh ra một ID duy nhất:
6464
6565
```js [[1, 4, "passwordHintId"]]
6666
import { useId } from 'react';
@@ -70,7 +70,7 @@ function PasswordField() {
7070
// ...
7171
```
7272
73-
You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different attributes:
73+
Sau đó bạn có thể truyền <CodeStep step={1}>ID đã sinh ra</CodeStep> vào các thuộc tính khác:
7474
7575
```js [[1, 2, "passwordHintId"], [1, 3, "passwordHintId"]]
7676
<>
@@ -79,26 +79,26 @@ You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different at
7979
</>
8080
```
8181
82-
**Let's walk through an example to see when this is useful.**
82+
**Hãy xem một ví dụ để thấy khi nào nó hữu ích.**
8383
84-
[HTML accessibility attributes](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA) like [`aria-describedby`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-describedby) let you specify that two tags are related to each other. For example, you can specify that an element (like an input) is described by another element (like a paragraph).
84+
[HTML accessibility attributes](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA) như [`aria-describedby`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-describedby) cho bạn chỉ định rằng hai thẻ có liên quan với nhau. Ví dụ, bạn có thể chỉ định rằng một phần tử (như input) được mô tả bởi một phần tử khác (như đoạn văn).
8585
86-
In regular HTML, you would write it like this:
86+
Trong HTML thuần, bạn sẽ viết như sau:
8787
8888
```html {5,8}
8989
<label>
90-
Password:
90+
Mật khẩu:
9191
<input
9292
type="password"
9393
aria-describedby="password-hint"
9494
/>
9595
</label>
9696
<p id="password-hint">
97-
The password should contain at least 18 characters
97+
Mật khẩu nên chứa ít nhất 18 ký tự
9898
</p>
9999
```
100100
101-
However, hardcoding IDs like this is not a good practice in React. A component may be rendered more than once on the page--but IDs have to be unique! Instead of hardcoding an ID, generate a unique ID with `useId`:
101+
Tuy nhiên, việc hardcode ID như thế không phải là một practice tốt trong React. một component có thể được render nhiều lần trên trang--nhưng ID thì phải là duy nhất! Thay vì hardcode một ID, hãy sinh ra một ID duy nhất với `useId`:
102102
103103
```js {4,11,14}
104104
import { useId } from 'react';
@@ -108,21 +108,21 @@ function PasswordField() {
108108
return (
109109
<>
110110
<label>
111-
Password:
111+
Mật khẩu:
112112
<input
113113
type="password"
114114
aria-describedby={passwordHintId}
115115
/>
116116
</label>
117117
<p id={passwordHintId}>
118-
The password should contain at least 18 characters
118+
Mật khẩu nên chứa ít nhất 18 ký tự
119119
</p>
120120
</>
121121
);
122122
}
123123
```
124124
125-
Now, even if `PasswordField` appears multiple times on the screen, the generated IDs won't clash.
125+
Và giờ, ngay cả khi bạn render nhiều instance của `PasswordField`, các ID được sinh ra sẽ không bị trùng lặp:
126126
127127
<Sandpack>
128128
@@ -134,14 +134,14 @@ function PasswordField() {
134134
return (
135135
<>
136136
<label>
137-
Password:
137+
Mật khẩu:
138138
<input
139139
type="password"
140140
aria-describedby={passwordHintId}
141141
/>
142142
</label>
143143
<p id={passwordHintId}>
144-
The password should contain at least 18 characters
144+
Mật khẩu nên chứa ít nhất 18 ký tự
145145
</p>
146146
</>
147147
);
@@ -150,9 +150,9 @@ function PasswordField() {
150150
export default function App() {
151151
return (
152152
<>
153-
<h2>Choose password</h2>
153+
<h2>Tạo mật khẩu</h2>
154154
<PasswordField />
155-
<h2>Confirm password</h2>
155+
<h2>Xác nhận mật khẩu</h2>
156156
<PasswordField />
157157
</>
158158
);
@@ -165,33 +165,33 @@ input { margin: 5px; }
165165
166166
</Sandpack>
167167
168-
[Watch this video](https://www.youtube.com/watch?v=0dNzNcuEuOo) to see the difference in the user experience with assistive technologies.
168+
[Xem video này](https://www.youtube.com/watch?v=0dNzNcuEuOo) để thấy sự khác biệt trong trải nghiệm người dùng với các công nghệ hỗ trợ.
169169
170170
<Pitfall>
171171
172-
With [server rendering](/reference/react-dom/server), **`useId` requires an identical component tree on the server and the client**. If the trees you render on the server and the client don't match exactly, the generated IDs won't match.
172+
Với [server rendering](/reference/react-dom/server), **`useId` yêu cầu component tree phải giống hệt nhau trên server và client**. Nếu cây bạn render trên server client không khớp hoàn toàn, các ID được sinh ra sẽ không khớp.
173173
174174
</Pitfall>
175175
176176
<DeepDive>
177177
178-
#### Why is useId better than an incrementing counter? {/*why-is-useid-better-than-an-incrementing-counter*/}
178+
#### Tại sao useId tốt hơn một counter tăng dần {/*why-is-useid-better-than-an-incrementing-counter*/}
179179
180-
You might be wondering why `useId` is better than incrementing a global variable like `nextId++`.
180+
Bạn có thể sẽ thắc mắc tại sao `useId` lại tốt hơn là tăng một biến toàn cục như `nextId++`.
181181
182-
The primary benefit of `useId` is that React ensures that it works with [server rendering.](/reference/react-dom/server) During server rendering, your components generate HTML output. Later, on the client, [hydration](/reference/react-dom/client/hydrateRoot) attaches your event handlers to the generated HTML. For hydration to work, the client output must match the server HTML.
182+
Lợi ích chính của `useId` React đảm bảo rằng nó hoạt động với [server rendering.](/reference/react-dom/server) Trong server rendering, các component của bạn sinh ra output HTML. Sau đó, trên client, [hydration](/reference/react-dom/client/hydrateRoot) gắn các event handler của bạn vào HTML đã sinh ra. Để hydration hoạt động, output trên client phải khớp với HTML trên server.
183183
184-
This is very difficult to guarantee with an incrementing counter because the order in which the Client Components are hydrated may not match the order in which the server HTML was emitted. By calling `useId`, you ensure that hydration will work, and the output will match between the server and the client.
184+
Điều này quá khó để đảm bảo với một biến đếm tăng dần vì thứ tự mà các Client Component được hydrated có thể không khớp với thứ tự mà HTML trên server được sinh ra. Bằng cách gọi `useId`, bạn đảm bảo rằng hydration sẽ hoạt động, và output sẽ khớp giữa server client.
185185
186-
Inside React, `useId` is generated from the "parent path" of the calling component. This is why, if the client and the server tree are the same, the "parent path" will match up regardless of rendering order.
186+
Trong React, `useId` được sinh ra từ "parent path" của component gọi nó. Đây là lý do tại sao, nếu cây trên client và server giống nhau, "parent path" sẽ khớp với nhau bất kể thứ tự render.
187187
188188
</DeepDive>
189189
190190
---
191191
192-
### Generating IDs for several related elements {/*generating-ids-for-several-related-elements*/}
192+
### Sinh ID cho nhiều phần tử liên quan {/*generating-ids-for-several-related-elements*/}
193193
194-
If you need to give IDs to multiple related elements, you can call `useId` to generate a shared prefix for them:
194+
Nếu bạn cần gán ID cho nhiều phần tử liên quan, bạn có thể gọi `useId` để sinh ra một tiền tố (prefix) dùng chung cho chúng:
195195
196196
<Sandpack>
197197
@@ -202,29 +202,30 @@ export default function Form() {
202202
const id = useId();
203203
return (
204204
<form>
205-
<label htmlFor={id + '-firstName'}>First Name:</label>
206-
<input id={id + '-firstName'} type="text" />
207-
<hr />
208-
<label htmlFor={id + '-lastName'}>Last Name:</label>
205+
<label htmlFor={id + '-lastName'}>H:</label>
209206
<input id={id + '-lastName'} type="text" />
207+
<hr />
208+
<label htmlFor={id + '-firstName'}>Tên:</label>
209+
<input id={id + '-firstName'} type="text" />
210210
</form>
211211
);
212212
}
213213
```
214214
215215
```css
216+
label { min-width: 50px; display: inline-block; }
216217
input { margin: 5px; }
217218
```
218219
219220
</Sandpack>
220221
221-
This lets you avoid calling `useId` for every single element that needs a unique ID.
222+
Điều này giúp bạn tránh phải gọi `useId` cho từng phần tử cần ID duy nhất.
222223
223224
---
224225
225-
### Specifying a shared prefix for all generated IDs {/*specifying-a-shared-prefix-for-all-generated-ids*/}
226+
### Đặt tiền tố dùng chung cho tất cả ID được sinh ra {/*specifying-a-shared-prefix-for-all-generated-ids*/}
226227
227-
If you render multiple independent React applications on a single page, pass `identifierPrefix` as an option to your [`createRoot`](/reference/react-dom/client/createRoot#parameters) or [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) calls. This ensures that the IDs generated by the two different apps never clash because every identifier generated with `useId` will start with the distinct prefix you've specified.
228+
Nếu bạn render nhiều ứng dụng React độc lập trên một trang, hãy truyền `identifierPrefix` như một tuỳ chọn vào các lệnh gọi [`createRoot`](/reference/react-dom/client/createRoot#parameters) hoặc [`hydrateRoot`](/reference/react-dom/client/hydrateRoot). Điều này đảm bảo rằng các ID được sinh ra bởi hai ứng dụng khác nhau sẽ không bao giờ bị trùng lặp vì mỗi ID được sinh ra với `useId` sẽ bắt đầu với tiền tố riêng biệt mà bạn đã chỉ định.
228229
229230
<Sandpack>
230231
@@ -248,14 +249,14 @@ function PasswordField() {
248249
return (
249250
<>
250251
<label>
251-
Password:
252+
Mật khẩu:
252253
<input
253254
type="password"
254255
aria-describedby={passwordHintId}
255256
/>
256257
</label>
257258
<p id={passwordHintId}>
258-
The password should contain at least 18 characters
259+
Mật khẩu nên chứa ít nhất 18 ký tự
259260
</p>
260261
</>
261262
);
@@ -264,7 +265,7 @@ function PasswordField() {
264265
export default function App() {
265266
return (
266267
<>
267-
<h2>Choose password</h2>
268+
<h2>Tạo mật khẩu</h2>
268269
<PasswordField />
269270
</>
270271
);
@@ -307,9 +308,9 @@ input { margin: 5px; }
307308
308309
---
309310
310-
### Using the same ID prefix on the client and the server {/*using-the-same-id-prefix-on-the-client-and-the-server*/}
311+
### Sử dụng cùng một tiền tố ID trên client server {/*using-the-same-id-prefix-on-the-client-and-the-server*/}
311312
312-
If you [render multiple independent React apps on the same page](#specifying-a-shared-prefix-for-all-generated-ids), and some of these apps are server-rendered, make sure that the `identifierPrefix` you pass to the [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) call on the client side is the same as the `identifierPrefix` you pass to the [server APIs](/reference/react-dom/server) such as [`renderToPipeableStream`.](/reference/react-dom/server/renderToPipeableStream)
313+
Nếu bạn [render nhiều ứng dụng React độc lập trên cùng một trang](#specifying-a-shared-prefix-for-all-generated-ids), và một số trong các ứng dụng này được server-rendered, hãy đảm bảo rằng `identifierPrefix` bạn truyền vào lệnh gọi [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) ở phía client giống với `identifierPrefix` bạn truyền vào các [API server](/reference/react-dom/server) như [`renderToPipeableStream`.](/reference/react-dom/server/renderToPipeableStream)
313314
314315
```js
315316
// Server
@@ -333,4 +334,4 @@ const root = hydrateRoot(
333334
);
334335
```
335336
336-
You do not need to pass `identifierPrefix` if you only have one React app on the page.
337+
Bạn không cần truyền `identifierPrefix` nếu bạn chỉ có một ứng dụng React trên trang.

0 commit comments

Comments
 (0)