You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Translate up to line 170
* Translate up to line 176
* Translate up to line 180
* Translate up to line 182
* Translate up to line 184
* Translate up to line 219
* Complete
* Update styling and correct sentences for readability
Copy file name to clipboardExpand all lines: src/content/reference/react/useId.md
+33-33Lines changed: 33 additions & 33 deletions
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: useId
4
4
5
5
<Intro>
6
6
7
-
`useId`is a React Hook for generating unique IDs that can be passed to accessibility attributes.
7
+
`useId`は、アクセシビリティ属性に渡すことができる一意の ID を生成するための React フックです。
8
8
9
9
```js
10
10
constid=useId()
@@ -16,11 +16,11 @@ const id = useId()
16
16
17
17
---
18
18
19
-
## Reference {/*reference*/}
19
+
## リファレンス {/*reference*/}
20
20
21
21
### `useId()` {/*useid*/}
22
22
23
-
Call`useId`at the top level of your component to generate a unique ID:
23
+
コンポーネントのトップレベルで`useId`を呼び出して、一意の ID を生成します。
24
24
25
25
```js
26
26
import { useId } from'react';
@@ -30,35 +30,35 @@ function PasswordField() {
30
30
// ...
31
31
```
32
32
33
-
[See more examples below.](#usage)
33
+
[さらに例を見る](#usage)
34
34
35
-
#### Parameters {/*parameters*/}
35
+
#### 引数 {/*parameters*/}
36
36
37
-
`useId`does not take any parameters.
37
+
`useId`はパラメータを受け取りません。
38
38
39
-
#### Returns {/*returns*/}
39
+
#### 返り値 {/*returns*/}
40
40
41
-
`useId`returns a unique ID string associated with this particular `useId`call in this particular component.
41
+
`useId`は、特定のコンポーネント内でこの特定の `useId`の呼び出しに関連付けられた、一意の ID 文字列を返します。
42
42
43
-
#### Caveats {/*caveats*/}
43
+
#### 注意事項 {/*caveats*/}
44
44
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.
@@ -77,11 +77,11 @@ You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different at
77
77
</>
78
78
```
79
79
80
-
**Let's walk through an example to see when this is useful.**
80
+
**これがどのような場合に役立つかを、例を通してみてみましょう。**
81
81
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).
@@ -96,7 +96,7 @@ In regular HTML, you would write it like this:
96
96
</p>
97
97
```
98
98
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
+
ただし、このように ID をハードコードすることは、React ではおすすめできません。コンポーネントはページ上で複数回レンダー可能ですが、ID は一意である必要があります! ID をハードコードするのではなく、`useId` を使用して一意の ID を生成します。
100
100
101
101
```js {4,11,14}
102
102
import { useId } from'react';
@@ -120,7 +120,7 @@ function PasswordField() {
120
120
}
121
121
```
122
122
123
-
Now, even if `PasswordField`appears multiple times on the screen, the generated IDs won't clash.
123
+
これで、画面上に `PasswordField`が複数回表示される場合でも、生成される ID が同じになることはありません。
124
124
125
125
<Sandpack>
126
126
@@ -163,33 +163,33 @@ input { margin: 5px; }
163
163
164
164
</Sandpack>
165
165
166
-
[Watch this video](https://www.youtube.com/watch?v=0dNzNcuEuOo) to see the difference in the user experience with assistive technologies.
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 は一致しません。
171
171
172
172
</Pitfall>
173
173
174
174
<DeepDive>
175
175
176
-
#### Why is useId better than an incrementing counter? {/*why-is-useid-better-than-an-incrementing-counter*/}
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 形式の出力を生成します。その後クライアント上で、生成された HTML に、[ハイドレーション](/reference/react-dom/client/hydrateRoot)によって、イベントハンドラがアタッチされます。ハイドレーションが機能するには、クライアントの出力がサーバの HTML と一致する必要があります。
181
181
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
+
クライアントコンポーネントがハイドレートされる順序は、サーバ HTML が出力された順序と一致しない可能性があるため、これをインクリメントカウンタで保証することは非常に困難です。`useId` を呼び出すことで、ハイドレーションが機能し、サーバとクライアントの間で出力が一致することが保証されます。
183
183
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.
### Generating IDs for several related elements {/*generating-ids-for-several-related-elements*/}
190
+
### 複数の関連要素に対して ID を生成する {/*generating-ids-for-several-related-elements*/}
191
191
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`で生成した値を共有のプレフィックスとして使用できます。
193
193
194
194
<Sandpack>
195
195
@@ -216,13 +216,13 @@ input { margin: 5px; }
216
216
217
217
</Sandpack>
218
218
219
-
This lets you avoid calling `useId`for every single element that needs a unique ID.
219
+
これにより、一意の ID を必要とするすべての要素に対して `useId`を呼び出す必要がなくなります。
220
220
221
221
---
222
222
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*/}
224
224
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.
0 commit comments