diff --git a/src/content/reference/react/useId.md b/src/content/reference/react/useId.md index a77cf7a72..26be36bec 100644 --- a/src/content/reference/react/useId.md +++ b/src/content/reference/react/useId.md @@ -4,7 +4,7 @@ title: useId -`useId` is a React Hook for generating unique IDs that can be passed to accessibility attributes. +`useId` 是一個 React Hook,用於產生可傳遞給無障礙屬性(accessibility attributes)的唯一 ID。 ```js const id = useId() @@ -16,11 +16,11 @@ const id = useId() --- -## Reference {/*reference*/} +## 參考 {/*reference*/} ### `useId()` {/*useid*/} -Call `useId` at the top level of your component to generate a unique ID: +在元件的最上層呼叫 `useId`,以產生唯一 ID: ```js import { useId } from 'react'; @@ -30,37 +30,37 @@ function PasswordField() { // ... ``` -[See more examples below.](#usage) +[參考下方更多範例。](#usage) -#### Parameters {/*parameters*/} +#### 參數 {/*parameters*/} -`useId` does not take any parameters. +`useId` 不接受任何參數。 -#### Returns {/*returns*/} +#### 回傳值 {/*returns*/} -`useId` returns a unique ID string associated with this particular `useId` call in this particular component. +`useId` 回傳一個唯一的 ID 字串,與這個元件中此特定的 `useId` 呼叫相關聯。 -#### Caveats {/*caveats*/} +#### 注意事項 {/*caveats*/} -* `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. +* `useId` 是一個 Hook,所以你只能在**元件的最上層**或你自己的 Hook 中呼叫它。你不能在迴圈或條件式中呼叫它。如果有這個需求,請抽離出一個新的元件,並將 state 移到其中。 -* `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) +* `useId` **不應該被用來產生列表中的 key**。[key 應該根據你的資料來產生。](/learn/rendering-lists#where-to-get-your-key) -* `useId` currently cannot be used in [async Server Components](/reference/rsc/server-components#async-components-with-server-components). +* `useId` 目前無法在 [async 的 Server Component](/reference/rsc/server-components#async-components-with-server-components) 中使用。 --- -## Usage {/*usage*/} +## 用法 {/*usage*/} -**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) +**不要呼叫 `useId` 來產生列表中的 key。** [key 應該根據你的資料來產生。](/learn/rendering-lists#where-to-get-your-key) -### Generating unique IDs for accessibility attributes {/*generating-unique-ids-for-accessibility-attributes*/} +### 為無障礙屬性產生唯一 ID {/*generating-unique-ids-for-accessibility-attributes*/} -Call `useId` at the top level of your component to generate a unique ID: +在元件的最上層呼叫 `useId`,以產生唯一 ID: ```js [[1, 4, "passwordHintId"]] import { useId } from 'react'; @@ -70,7 +70,7 @@ function PasswordField() { // ... ``` -You can then pass the generated ID to different attributes: +接著你可以將產生的 ID傳遞給不同的屬性: ```js [[1, 2, "passwordHintId"], [1, 3, "passwordHintId"]] <> @@ -79,11 +79,11 @@ You can then pass the generated ID to different at ``` -**Let's walk through an example to see when this is useful.** +**讓我們透過一個範例來了解這在什麼時候有用。** -[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). +[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)由另一個元素(像是段落)來描述。 -In regular HTML, you would write it like this: +在一般的 HTML 中,你會這樣寫: ```html {5,8}