Skip to content

Commit f260802

Browse files
Java-S12138awxiaoxian2020Yucohny
authored
docs(cn): translate reference/react/useId into Chinese (#1108)
Co-authored-by: Xavi Lee <awxiaoxian2020@163.com> Co-authored-by: Yucohny <79147654+Yucohny@users.noreply.github.com>
1 parent b906585 commit f260802

File tree

1 file changed

+47
-47
lines changed

1 file changed

+47
-47
lines changed

src/content/reference/react/useId.md

Lines changed: 47 additions & 47 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,可以生成传递给无障碍属性的唯一 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,35 +30,35 @@ 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
4949
---
5050
51-
## Usage {/*usage*/}
51+
## 用法 {/*usage*/}
5252
5353
<Pitfall>
5454
55-
**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)
55+
**不要使用 `useId` 来生成列表中的 key**。[key 应该由你的数据生成](/learn/rendering-lists#where-to-get-your-key)
5656
5757
</Pitfall>
5858
59-
### Generating unique IDs for accessibility attributes {/*generating-unique-ids-for-accessibility-attributes*/}
59+
### 为无障碍属性生成唯一 ID {/*generating-unique-ids-for-accessibility-attributes*/}
6060
61-
Call `useId` at the top level of your component to generate a unique ID:
61+
在组件的顶层调用 `useId` 生成唯一 ID:
6262
6363
```js [[1, 4, "passwordHintId"]]
6464
import { useId } from 'react';
@@ -68,7 +68,7 @@ function PasswordField() {
6868
// ...
6969
```
7070
71-
You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different attributes:
71+
你可以将 <CodeStep step={1}>生成的 ID</CodeStep> 传递给不同的属性:
7272
7373
```js [[1, 2, "passwordHintId"], [1, 3, "passwordHintId"]]
7474
<>
@@ -77,26 +77,26 @@ You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different at
7777
</>
7878
```
7979
80-
**Let's walk through an example to see when this is useful.**
80+
**让我们通过一个例子,看看这个什么时候有用**。
8181
82-
[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).
82+
如 [`aria-describedby`](https://developer.mozilla.org/zh-CN/docs/Web/Accessibility/ARIA/Attributes/aria-describedby) 这样的 [HTML 无障碍属性](https://developer.mozilla.org/zh-CN/docs/Web/Accessibility/ARIA) 允许你指定两个标签之间的关系。例如,你可以指定一个元素(比如输入框)由另一个元素(比如段落)描述。
8383
84-
In regular HTML, you would write it like this:
84+
在常规的 HTML 中,你会这样写:
8585
8686
```html {5,8}
8787
<label>
88-
Password:
88+
密码:
8989
<input
9090
type="password"
9191
aria-describedby="password-hint"
9292
/>
9393
</label>
9494
<p id="password-hint">
95-
The password should contain at least 18 characters
95+
密码应该包含至少 18 个字符
9696
</p>
9797
```
9898
99-
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`:
99+
然而,在 React 中直接编写 ID 并不是一个好的习惯。一个组件可能会在页面上渲染多次,但是 ID 必须是唯一的!不要使用自己编写的 ID,而是使用 `useId` 生成唯一的 ID。
100100
101101
```js {4,11,14}
102102
import { useId } from 'react';
@@ -106,21 +106,21 @@ function PasswordField() {
106106
return (
107107
<>
108108
<label>
109-
Password:
109+
密码:
110110
<input
111111
type="password"
112112
aria-describedby={passwordHintId}
113113
/>
114114
</label>
115115
<p id={passwordHintId}>
116-
The password should contain at least 18 characters
116+
密码应该包含至少 18 个字符
117117
</p>
118118
</>
119119
);
120120
}
121121
```
122122
123-
Now, even if `PasswordField` appears multiple times on the screen, the generated IDs won't clash.
123+
现在,即使 `PasswordField` 多次出现在屏幕上,生成的 ID 并不会冲突。
124124
125125
<Sandpack>
126126
@@ -132,14 +132,14 @@ function PasswordField() {
132132
return (
133133
<>
134134
<label>
135-
Password:
135+
密码:
136136
<input
137137
type="password"
138138
aria-describedby={passwordHintId}
139139
/>
140140
</label>
141141
<p id={passwordHintId}>
142-
The password should contain at least 18 characters
142+
密码应该包含至少 18 个字符
143143
</p>
144144
</>
145145
);
@@ -148,9 +148,9 @@ function PasswordField() {
148148
export default function App() {
149149
return (
150150
<>
151-
<h2>Choose password</h2>
151+
<h2>输入密码</h2>
152152
<PasswordField />
153-
<h2>Confirm password</h2>
153+
<h2>验证密码</h2>
154154
<PasswordField />
155155
</>
156156
);
@@ -163,33 +163,33 @@ input { margin: 5px; }
163163
164164
</Sandpack>
165165
166-
[Watch this video](https://www.youtube.com/watch?v=0dNzNcuEuOo) to see the difference in the user experience with assistive technologies.
166+
[请看这个视频](https://www.youtube.com/watch?v=0dNzNcuEuOo),了解辅助技术所提供的用户体验的差异。
167167
168168
<Pitfall>
169169
170-
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.
170+
[使用服务端渲染时](/reference/react-dom/server)**`useId` 需要在服务器和客户端上有相同的组件树**。如果你在服务器和客户端上渲染的树不完全匹配,生成的 ID 将不匹配。
171171
172172
</Pitfall>
173173
174174
<DeepDive>
175175
176-
#### Why is useId better than an incrementing counter? {/*why-is-useid-better-than-an-incrementing-counter*/}
176+
#### 为什么 useId 比递增计数器更好? {/*why-is-useid-better-than-an-incrementing-counter*/}
177177
178-
You might be wondering why `useId` is better than incrementing a global variable like `nextId++`.
178+
你可能想知道为什么使用 `useId` 比增加全局变量(如 `nextId++`)更好。
179179
180-
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.
180+
`useId` 的主要好处是 React 确保它能够与 [服务端渲染](/reference/react-dom/server)一起工作。 在服务器渲染期间,你的组件生成输出 HTML。随后,在客户端,[hydration](/reference/react-dom/client/hydrateRoot) 会将你的事件处理程序附加到生成的 HTML 上。由于 hydration,客户端必须匹配服务器输出的 HTML
181181
182-
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.
182+
使用递增计数器非常难以保证这一点,因为客户端组件被 hydrate 处理后的顺序可能与服务器 HTML 发出的顺序不匹配。通过调用 `useId`,你可以确保 hydration 正常工作,并且服务器和客户端之间的输出将匹配。
183183
184-
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.
184+
React 内部,调用组件的“父路径”生成 `useId`。这就是为什么如果客户端和服务器的树相同,不管渲染顺序如何,“父路径”始终都匹配。
185185
186186
</DeepDive>
187187
188188
---
189189
190-
### Generating IDs for several related elements {/*generating-ids-for-several-related-elements*/}
190+
### 为多个相关元素生成 ID {/*generating-ids-for-several-related-elements*/}
191191
192-
If you need to give IDs to multiple related elements, you can call `useId` to generate a shared prefix for them:
192+
如果你需要为多个相关元素生成 ID,可以调用 `useId` 来为它们生成共同的前缀:
193193
194194
<Sandpack>
195195
@@ -200,10 +200,10 @@ export default function Form() {
200200
const id = useId();
201201
return (
202202
<form>
203-
<label htmlFor={id + '-firstName'}>First Name:</label>
203+
<label htmlFor={id + '-firstName'}>名字:</label>
204204
<input id={id + '-firstName'} type="text" />
205205
<hr />
206-
<label htmlFor={id + '-lastName'}>Last Name:</label>
206+
<label htmlFor={id + '-lastName'}>姓氏:</label>
207207
<input id={id + '-lastName'} type="text" />
208208
</form>
209209
);
@@ -216,13 +216,13 @@ input { margin: 5px; }
216216
217217
</Sandpack>
218218
219-
This lets you avoid calling `useId` for every single element that needs a unique ID.
219+
可以使你避免为每个需要唯一 ID 的元素调用 `useId`
220220
221221
---
222222
223-
### Specifying a shared prefix for all generated IDs {/*specifying-a-shared-prefix-for-all-generated-ids*/}
223+
### 为所有生成的 ID 指定共享前缀 {/*specifying-a-shared-prefix-for-all-generated-ids*/}
224224
225-
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.
225+
如果你在单个页面上渲染多个独立的 React 应用程序,请在 [`createRoot`](/reference/react-dom/client/createRoot#parameters) [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) 调用中将 `identifierPrefix` 作为选项传递。这确保了由两个不同应用程序生成的 ID 永远不会冲突,因为使用 `useId` 生成的每个 ID 都将以你指定的不同前缀开头。
226226
227227
<Sandpack>
228228
@@ -242,18 +242,18 @@ import { useId } from 'react';
242242

243243
function PasswordField() {
244244
const passwordHintId = useId();
245-
console.log('Generated identifier:', passwordHintId)
245+
console.log('生成的 ID:', passwordHintId)
246246
return (
247247
<>
248248
<label>
249-
Password:
249+
密码:
250250
<input
251251
type="password"
252252
aria-describedby={passwordHintId}
253253
/>
254254
</label>
255255
<p id={passwordHintId}>
256-
The password should contain at least 18 characters
256+
密码应该包含至少 18 个字符
257257
</p>
258258
</>
259259
);
@@ -262,7 +262,7 @@ function PasswordField() {
262262
export default function App() {
263263
return (
264264
<>
265-
<h2>Choose password</h2>
265+
<h2>输入密码</h2>
266266
<PasswordField />
267267
</>
268268
);

0 commit comments

Comments
 (0)