Skip to content

Commit 049cf0f

Browse files
docs(reference): translate useId
1 parent 19692fa commit 049cf0f

1 file changed

Lines changed: 37 additions & 37 deletions

File tree

src/content/reference/react/useId.md

Lines changed: 37 additions & 37 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` 是一個 React Hook,用於產生可傳遞給無障礙屬性(accessibility attributes)的唯一 ID。
88

99
```js
1010
const id = useId()
@@ -16,11 +16,11 @@ const id = useId()
1616

1717
---
1818

19-
## Reference {/*reference*/}
19+
## 參考 {/*reference*/}
2020

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

23-
Call `useId` at the top level of your component to generate a unique ID:
23+
在元件的最上層呼叫 `useId`,以產生唯一 ID:
2424

2525
```js
2626
import { useId } from 'react';
@@ -30,37 +30,37 @@ function PasswordField() {
3030
// ...
3131
```
3232
33-
[See more examples below.](#usage)
33+
[參考下方更多範例。](#usage)
3434
35-
#### Parameters {/*parameters*/}
35+
#### 參數 {/*parameters*/}
3636
37-
`useId` does not take any parameters.
37+
`useId` 不接受任何參數。
3838
39-
#### Returns {/*returns*/}
39+
#### 回傳值 {/*returns*/}
4040
41-
`useId` returns a unique ID string associated with this particular `useId` call in this particular component.
41+
`useId` 回傳一個唯一的 ID 字串,與這個元件中此特定的 `useId` 呼叫相關聯。
4242
43-
#### Caveats {/*caveats*/}
43+
#### 注意事項 {/*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` 是一個 Hook,所以你只能在**元件的最上層**或你自己的 Hook 中呼叫它。你不能在迴圈或條件式中呼叫它。如果有這個需求,請抽離出一個新的元件,並將 state 移到其中。
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` **不應該被用來產生列表中的 key**。[key 應該根據你的資料來產生。](/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` 目前無法在 [async Server Component](/reference/rsc/server-components#async-components-with-server-components) 中使用。
5050
5151
---
5252
53-
## Usage {/*usage*/}
53+
## 用法 {/*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+
**不要呼叫 `useId` 來產生列表中的 key。** [key 應該根據你的資料來產生。](/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+
### 為無障礙屬性產生唯一 ID {/*generating-unique-ids-for-accessibility-attributes*/}
6262
63-
Call `useId` at the top level of your component to generate a unique ID:
63+
在元件的最上層呼叫 `useId`,以產生唯一 ID:
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+
接著你可以將<CodeStep step={1}>產生的 ID</CodeStep>傳遞給不同的屬性:
7474
7575
```js [[1, 2, "passwordHintId"], [1, 3, "passwordHintId"]]
7676
<>
@@ -79,11 +79,11 @@ 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+
**讓我們透過一個範例來了解這在什麼時候有用。**
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 無障礙屬性](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA),例如 [`aria-describedby`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-describedby),讓你能指定兩個標籤彼此相關。舉例來說,你可以指定某個元素(像是 input)由另一個元素(像是段落)來描述。
8585
86-
In regular HTML, you would write it like this:
86+
在一般的 HTML 中,你會這樣寫:
8787
8888
```html {5,8}
8989
<label>
@@ -98,7 +98,7 @@ In regular HTML, you would write it like this:
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+
然而,像這樣把 ID 寫死在 React 中並不是好的做法。一個元件在頁面上可能會被渲染不只一次——但 ID 必須是唯一的!與其把 ID 寫死,不如使用 `useId` 來產生唯一 ID:
102102
103103
```js {4,11,14}
104104
import { useId } from 'react';
@@ -122,7 +122,7 @@ function PasswordField() {
122122
}
123123
```
124124
125-
Now, even if `PasswordField` appears multiple times on the screen, the generated IDs won't clash.
125+
如此一來,即使 `PasswordField` 在畫面上出現多次,產生的 ID 也不會互相衝突。
126126
127127
<Sandpack>
128128
@@ -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+
[觀看這部影片](https://www.youtube.com/watch?v=0dNzNcuEuOo),了解使用輔助技術時使用者體驗的差異。
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+
使用 [server 渲染](/reference/react-dom/server)時,**`useId` 要求 server client 上的元件樹完全相同**。如果你在 server client 上渲染的樹不完全相符,產生的 ID 也不會相符。
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+
#### 為什麼 useId 比遞增的計數器更好? {/*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+
你可能會好奇,為什麼 `useId` 比遞增一個全域變數(像是 `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+
`useId` 主要的好處在於 React 確保它能與 [server 渲染](/reference/react-dom/server)一起運作。在 server 渲染期間,你的元件會產生 HTML 輸出。之後在 client 端,[hydration](/reference/react-dom/client/hydrateRoot) 會將你的事件處理函式附加到產生的 HTML 上。為了讓 hydration 正常運作,client 的輸出必須與 server HTML 相符。
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+
要用遞增的計數器來保證這一點非常困難,因為 Client Component 被 hydrate 的順序,可能與 server HTML 輸出的順序不一致。透過呼叫 `useId`,你可以確保 hydration 正常運作,並讓 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+
React 內部,`useId` 是根據呼叫它的元件的「父層路徑(parent path)」產生的。這就是為什麼,只要 client server 的樹相同,無論渲染順序為何,「父層路徑」都會吻合。
187187
188188
</DeepDive>
189189
190190
---
191191
192-
### Generating IDs for several related elements {/*generating-ids-for-several-related-elements*/}
192+
### 為多個相關的元素產生 ID {/*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+
如果你需要為多個相關的元素賦予 ID,可以呼叫 `useId` 來為它們產生一個共用的前綴:
195195
196196
<Sandpack>
197197
@@ -218,13 +218,13 @@ input { margin: 5px; }
218218
219219
</Sandpack>
220220
221-
This lets you avoid calling `useId` for every single element that needs a unique ID.
221+
這讓你不必為每一個需要唯一 ID 的元素都各自呼叫一次 `useId`
222222
223223
---
224224
225-
### Specifying a shared prefix for all generated IDs {/*specifying-a-shared-prefix-for-all-generated-ids*/}
225+
### 為所有產生的 ID 指定共用前綴 {/*specifying-a-shared-prefix-for-all-generated-ids*/}
226226
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.
227+
如果你在單一頁面上渲染多個獨立的 React 應用程式,請將 `identifierPrefix` 作為選項傳遞給你的 [`createRoot`](/reference/react-dom/client/createRoot#parameters) [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) 呼叫。這能確保兩個不同應用程式所產生的 ID 永遠不會衝突,因為每個使用 `useId` 產生的識別碼,都會以你指定的、彼此不同的前綴開頭。
228228
229229
<Sandpack>
230230
@@ -307,9 +307,9 @@ input { margin: 5px; }
307307
308308
---
309309
310-
### Using the same ID prefix on the client and the server {/*using-the-same-id-prefix-on-the-client-and-the-server*/}
310+
### 在 client 與 server 上使用相同的 ID 前綴 {/*using-the-same-id-prefix-on-the-client-and-the-server*/}
311311
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)
312+
如果你[在同一個頁面上渲染多個獨立的 React 應用程式](#specifying-a-shared-prefix-for-all-generated-ids),而其中某些應用程式是 server 渲染的,請確保你在 client 端傳遞給 [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) 呼叫的 `identifierPrefix`,與你傳遞給 [server API](/reference/react-dom/server)(例如 [`renderToPipeableStream`](/reference/react-dom/server/renderToPipeableStream))的 `identifierPrefix` 相同。
313313
314314
```js
315315
// Server
@@ -333,4 +333,4 @@ const root = hydrateRoot(
333333
);
334334
```
335335
336-
You do not need to pass `identifierPrefix` if you only have one React app on the page.
336+
如果你的頁面上只有一個 React 應用程式,則不需要傳遞 `identifierPrefix`

0 commit comments

Comments
 (0)