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
Copy file name to clipboardExpand all lines: src/content/reference/react/useId.md
+29-25Lines changed: 29 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,8 @@ title: useId
6
6
7
7
`useId` is a React Hook for generating unique IDs that can be passed to accessibility attributes.
8
8
9
+
`useId` — це хук для генерації унікальних ID, які можуть передаватись як атрибути доступності.
10
+
9
11
```js
10
12
constid=useId()
11
13
```
@@ -20,7 +22,7 @@ const id = useId()
20
22
21
23
### `useId()` {/*useid*/}
22
24
23
-
Call`useId`at the top level of your component to generate a unique ID:
25
+
Викличте`useId`на верхньому рівні вашого компонента, щоб згенерувати унікальне ID:
24
26
25
27
```js
26
28
import { useId } from'react';
@@ -30,37 +32,38 @@ function PasswordField() {
30
32
// ...
31
33
```
32
34
33
-
[See more examples below.](#usage)
35
+
[Перегляньте більше прикладів нижче.](#usage)
34
36
35
-
#### Parameters {/*parameters*/}
37
+
#### Параметри {/*parameters*/}
36
38
37
-
`useId`does not take any parameters.
39
+
`useId`не приймає ніяких параметрів.
38
40
39
-
#### Returns {/*returns*/}
41
+
#### Результат {/*returns*/}
40
42
41
-
`useId`returns a unique ID string associated with this particular`useId`call in this particular component.
43
+
`useId`повертає унікальну ID стрінгу, пов'язану з конкретним запитом`useId`в цьому конкретньому компоненті.
42
44
43
-
#### Caveats {/*caveats*/}
45
+
#### Застереження {/*caveats*/}
44
46
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.
47
+
* `useId`— це хук, тож він може бути викликаний тільки **на верхньому рівні вашого компонента** або у вашому власному Хуці. Ви не можете викликати його в циклах або умовах. Якщо ж є така потреба, то витягніть новий компонент та перемістіть в нього стан.
46
48
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)
49
+
* `useId` **не повинно використовуватись для генерації ключів** у списках. [Ключі повинні генеруватись з ваших даних.](/learn/rendering-lists#where-to-get-your-key)
48
50
49
-
* `useId`currently cannot be used in [async Server Components](/reference/rsc/server-components#async-components-with-server-components).
51
+
* `useId`на данний момент не може бути використано в [async Server Components](/reference/rsc/server-components#async-components-with-server-components).
50
52
51
53
---
52
54
53
-
## Usage {/*usage*/}
55
+
## Використання {/*usage*/}
54
56
55
57
<Pitfall>
56
58
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)
59
+
**Не викликайте `useId` для генерації ключів у списку.** [Ключі повинні генеруватись з ваших даних.](/learn/rendering-lists#where-to-get-your-key)
60
+
58
61
59
62
</Pitfall>
60
63
61
-
### Generating unique IDs for accessibility attributes {/*generating-unique-ids-for-accessibility-attributes*/}
64
+
### Генеруйте унікальні ID для атрибутів доступності {/*generating-unique-ids-for-accessibility-attributes*/}
62
65
63
-
Call`useId`at the top level of your component to generate a unique ID:
66
+
Викликайте`useId`на верхньому рівні вашої компоненти для генерації унікального ID:
64
67
65
68
```js [[1, 4, "passwordHintId"]]
66
69
import { useId } from'react';
@@ -70,7 +73,7 @@ function PasswordField() {
70
73
// ...
71
74
```
72
75
73
-
You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different attributes:
76
+
Далі ви можете передати <CodeStep step={1}>generated ID</CodeStep> до різних атрибутів:
@@ -79,11 +82,11 @@ You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different at
79
82
</>
80
83
```
81
84
82
-
**Let's walk through an example to see when this is useful.**
85
+
**Давайте розглянемо приклади, коли це може бути корисно.**
83
86
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).
87
+
[Атрибути доступності 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) описан іншим компонентом (такий як параграф).
85
88
86
-
In regular HTML, you would write it like this:
89
+
В звичайному HTML, ви би написали наступне:
87
90
88
91
```html {5,8}
89
92
<label>
@@ -98,7 +101,7 @@ In regular HTML, you would write it like this:
98
101
</p>
99
102
```
100
103
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`:
104
+
Однак, такий хардкод ID не найкраща практика в React. Компонент може бути зарендерений на сторінці більш ніж один раз — але ID повинні бути унікальні! Замість того, щоб хардкодити ID, зренеруйте унікальне за допомогою `useId`:
102
105
103
106
```js {4,11,14}
104
107
import { useId } from'react';
@@ -115,14 +118,14 @@ function PasswordField() {
115
118
/>
116
119
</label>
117
120
<p id={passwordHintId}>
118
-
The password should contain at least 18characters
121
+
Пароль повинен буди довжиною не меньш ніж 18символів
119
122
</p>
120
123
</>
121
124
);
122
125
}
123
126
```
124
127
125
-
Now, even if`PasswordField`appears multiple times on the screen, the generated IDs won't clash.
128
+
Тож, навіть якщо`PasswordField`з'явиться на сторінці багато разів, згенеровані ID не конфліктуватимуть.
126
129
127
130
<Sandpack>
128
131
@@ -141,7 +144,7 @@ function PasswordField() {
141
144
/>
142
145
</label>
143
146
<p id={passwordHintId}>
144
-
The password should contain at least 18characters
147
+
Пароль повинен буди довжиною не меньш ніж 18символів
145
148
</p>
146
149
</>
147
150
);
@@ -150,9 +153,9 @@ function PasswordField() {
150
153
exportdefaultfunctionApp() {
151
154
return (
152
155
<>
153
-
<h2>Choose password</h2>
156
+
<h2>Оберіть пароль</h2>
154
157
<PasswordField />
155
-
<h2>Confirm password</h2>
158
+
<h2>Підтвердіть пароль</h2>
156
159
<PasswordField />
157
160
</>
158
161
);
@@ -165,11 +168,12 @@ input { margin: 5px; }
165
168
166
169
</Sandpack>
167
170
168
-
[Watch this video](https://www.youtube.com/watch?v=0dNzNcuEuOo) to see the difference in the user experience with assistive technologies.
171
+
[Перегляньте відео](https://www.youtube.com/watch?v=0dNzNcuEuOo), щоб побачити різницю користувацького досвіду з дороміжними технологіями.
169
172
170
173
<Pitfall>
171
174
172
175
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.
176
+
Разом з [серверним рендерінгом](/reference/react-dom/server), **`useId` потребує ідентичного дерева компонент на сервері та на клієнті**. If the trees you render on the server and the client don't match exactly, the generated IDs won't match.
0 commit comments