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
+51-51Lines changed: 51 additions & 51 deletions
Display the source diff
Display the rich diff
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`je React Hook za generisanje jedinstvenih ID-eva koji se mogu proslediti atributima pristupačnosti.
8
8
9
9
```js
10
10
constid=useId()
@@ -20,7 +20,7 @@ const id = useId()
20
20
21
21
### `useId()` {/*useid*/}
22
22
23
-
Call`useId`at the top level of your component to generate a unique ID:
23
+
Pozovite`useId`na vrhu vaše komponente da biste generisali jedinstveni ID:
24
24
25
25
```js
26
26
import { useId } from'react';
@@ -30,37 +30,37 @@ function PasswordField() {
30
30
// ...
31
31
```
32
32
33
-
[See more examples below.](#usage)
33
+
[Pogledajte još primera ispod.](#usage)
34
34
35
-
#### Parameters {/*parameters*/}
35
+
#### Parametri {/*parameters*/}
36
36
37
-
`useId`does not take any parameters.
37
+
`useId`ne prima nikakve parametre.
38
38
39
-
#### Returns {/*returns*/}
39
+
#### Povratne vrednosti {/*returns*/}
40
40
41
-
`useId`returns a unique ID string associated with this particular `useId`call in this particular component.
41
+
`useId`vraća jedinstveni ID string asociran za specifični `useId`poziv u specifičnoj komponenti.
42
42
43
-
#### Caveats {/*caveats*/}
43
+
#### Upozorenja {/*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.
45
+
* `useId`je Hook, pa ga možete pozvati samo **na vrhu vaše komponente** ili vaših Hook-ova. Ne možete ga pozvati unutar petlji i uslova. Ako vam je to potrebno, izdvojite novu komponentu i pomerite state u nju.
46
46
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` **ne bi trebao da se koristi za generisanje ključeva** u listi. [Ključevi trebaju biti generisani na osnovu vaših podataka.](/learn/rendering-lists#where-to-get-your-key)
48
48
49
-
* `useId`currently cannot be used in [async Server Components](/reference/rsc/server-components#async-components-with-server-components).
49
+
* `useId`se trenutno ne može koristiti u [asinhronim Server Components](/reference/rsc/server-components#async-components-with-server-components).
50
50
51
51
---
52
52
53
-
## Usage {/*usage*/}
53
+
## Upotreba {/*usage*/}
54
54
55
55
<Pitfall>
56
56
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
+
**Nemojte pozivati `useId`da biste generisali ključeve u listi.** [Ključevi trebaju biti generisani na osnovu vaših podataka.](/learn/rendering-lists#where-to-get-your-key)
58
58
59
59
</Pitfall>
60
60
61
-
### Generating unique IDs for accessibility attributes {/*generating-unique-ids-for-accessibility-attributes*/}
61
+
### Generisanje jedinstvenih ID-eva za atribute pristupačnosti {/*generating-unique-ids-for-accessibility-attributes*/}
62
62
63
-
Call`useId`at the top level of your component to generate a unique ID:
63
+
Pozovite`useId`na vrhu vaše komponente da biste generisali jedinstveni ID:
64
64
65
65
```js [[1, 4, "passwordHintId"]]
66
66
import { useId } from'react';
@@ -70,7 +70,7 @@ function PasswordField() {
70
70
// ...
71
71
```
72
72
73
-
You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different attributes:
73
+
Onda možete proslediti <CodeStep step={1}>generisani ID</CodeStep> u različite atribute:
@@ -79,26 +79,26 @@ You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different at
79
79
</>
80
80
```
81
81
82
-
**Let's walk through an example to see when this is useful.**
82
+
**Prođimo kroz primer da vidimo kada je ovo korisno.**
83
83
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 atributi pristupačnosti](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA) kao što je [`aria-describedby`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-describedby) vam omogućavaju da specificirate dva tag-a koja su povezana. Na primer, možete specificirati da je element (kao što je input) opisan drugim elementom (kao što je paragraph).
85
85
86
-
In regular HTML, you would write it like this:
86
+
U običnom HTML-u napisali biste ovako nešto:
87
87
88
88
```html {5,8}
89
89
<label>
90
-
Password:
90
+
Šifra:
91
91
<input
92
92
type="password"
93
93
aria-describedby="password-hint"
94
94
/>
95
95
</label>
96
96
<p id="password-hint">
97
-
The password should contain at least 18characters
97
+
Šifra treba da sadrži barem 18karaktera
98
98
</p>
99
99
```
100
100
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
+
Međutim, hardkodirani ID-evi poput ovih nisu dobra praksa u React-u. Komponenta može biti renderovana više od jednom na stranici--ali ID-evi moraju biti jedinstveni! Umesto da hardkodirate ID, generišite jedinstveni ID sa`useId`:
102
102
103
103
```js {4,11,14}
104
104
import { useId } from'react';
@@ -108,21 +108,21 @@ function PasswordField() {
108
108
return (
109
109
<>
110
110
<label>
111
-
Password:
111
+
Šifra:
112
112
<input
113
113
type="password"
114
114
aria-describedby={passwordHintId}
115
115
/>
116
116
</label>
117
117
<p id={passwordHintId}>
118
-
The password should contain at least 18characters
118
+
Šifra treba da sadrži barem 18karaktera
119
119
</p>
120
120
</>
121
121
);
122
122
}
123
123
```
124
124
125
-
Now, even if `PasswordField`appears multiple times on the screen, the generated IDs won't clash.
125
+
Sada, čak iako se `PasswordField`pojavi više puta na ekranu, generisani ID-evi se neće preklapati.
126
126
127
127
<Sandpack>
128
128
@@ -134,14 +134,14 @@ function PasswordField() {
134
134
return (
135
135
<>
136
136
<label>
137
-
Password:
137
+
Šifra:
138
138
<input
139
139
type="password"
140
140
aria-describedby={passwordHintId}
141
141
/>
142
142
</label>
143
143
<p id={passwordHintId}>
144
-
The password should contain at least 18characters
144
+
Šifra treba da sadrži barem 18karaktera
145
145
</p>
146
146
</>
147
147
);
@@ -150,9 +150,9 @@ function PasswordField() {
150
150
exportdefaultfunctionApp() {
151
151
return (
152
152
<>
153
-
<h2>Choose password</h2>
153
+
<h2>Unesi šifru</h2>
154
154
<PasswordField />
155
-
<h2>Confirm password</h2>
155
+
<h2>Potvrdi šifru</h2>
156
156
<PasswordField />
157
157
</>
158
158
);
@@ -165,33 +165,33 @@ input { margin: 5px; }
165
165
166
166
</Sandpack>
167
167
168
-
[Watch this video](https://www.youtube.com/watch?v=0dNzNcuEuOo) to see the difference in the user experience with assistive technologies.
168
+
[Pogledajte ovaj video](https://www.youtube.com/watch?v=0dNzNcuEuOo) da biste uočili razliku u korisničkom iskustvu sa pomoćnim tehnologijama.
169
169
170
170
<Pitfall>
171
171
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
+
Sa [renderovanjem na serveru](/reference/react-dom/server), **`useId`zahteva identično stablo komponente na serveru i na klijentu**. Ako se stabla koja renderujete na serveru i klijentu ne poklapaju, ni generisani ID-evi se neće poklapati.
173
173
174
174
</Pitfall>
175
175
176
176
<DeepDive>
177
177
178
-
#### Why is useId better than an incrementing counter? {/*why-is-useid-better-than-an-incrementing-counter*/}
178
+
#### Zašto je useId bolji od inkrementalnog brojača? {/*why-is-useid-better-than-an-incrementing-counter*/}
179
179
180
-
You might be wondering why`useId`is better than incrementing a global variable like`nextId++`.
180
+
Možda se pitate zašto je`useId`bolji od inkrementiranja globalne promenljive poput`nextId++`.
181
181
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
+
Primarna korist od `useId`je da React garantuje da radi sa [renderovanjem na serveru](/reference/react-dom/server). Tokom renderovanja na serveru, vaše komponente generišu HTML izlaz. Kasnije, na klijentu, [hidratacijom](/reference/react-dom/client/hydrateRoot) se povezuju vaši event handler-i sa generisanim HTML-om. Da bi hidratacija radila, klijentski izlaz se mora poklapati sa serverskim HTML-om.
183
183
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
+
Ovo je teško garantovati sa inkrementalnim brojačem jer se redosled u kom se Client Components hidratizuju možda neće poklapati sa redosledom u kom je emitovan HTML sa servera. Pozivanjem `useId` osiguravate da će hidratacija raditi i da će se izlaz servera poklapati sa klijentskim.
185
185
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
+
Unutar React-a, `useId`je generisan na osnovu "roditeljske putanje" pozivajuće komponente. Zbog toga, ako se klijentsko i serversko stablo poklapaju, "roditeljska putanja" će se poklapati bez obzira na redosled.
187
187
188
188
</DeepDive>
189
189
190
190
---
191
191
192
-
### Generating IDs for several related elements {/*generating-ids-for-several-related-elements*/}
192
+
### Generisanje ID-eva za više povezanih elemenata {/*generating-ids-for-several-related-elements*/}
193
193
194
-
If you need to give IDs to multiple related elements, you can call `useId`to generate a shared prefix for them:
194
+
Ako vam je potrebno da napravite ID-eve za više povezanih elemenata, možete pozvati `useId`da generišete deljeni prefiks za njih:
195
195
196
196
<Sandpack>
197
197
@@ -202,10 +202,10 @@ export default function Form() {
This lets you avoid calling `useId` for every single element that needs a unique ID.
221
+
Ovo vam omogućava da izbegnete pozivanje `useId`-a za svaki pojedinačni element kojem je potreban jedinstveni ID.
222
222
223
223
---
224
224
225
-
### Specifying a shared prefix for all generated IDs {/*specifying-a-shared-prefix-for-all-generated-ids*/}
225
+
### Specificiranje deljenog prefiksa za sve generisane ID-eve {/*specifying-a-shared-prefix-for-all-generated-ids*/}
226
226
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
+
Ako renderujete više nezavisnih React aplikacija na jednog stranici, prosledite`identifierPrefix`u vaše [`createRoot`](/reference/react-dom/client/createRoot#parameters) ili [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) pozive. Ovo osigurava da se ID-evi generisani u dve različite aplikacije nikad neće preklapati jer će svaki identifikator generisan sa `useId`početi sa različitim prefiksom koji ste specificirali.
228
228
229
229
<Sandpack>
230
230
@@ -244,18 +244,18 @@ import { useId } from 'react';
### Using the same ID prefix on the client and the server {/*using-the-same-id-prefix-on-the-client-and-the-server*/}
310
+
### Upotreba istog ID prefiksa na klijentu i serveru {/*using-the-same-id-prefix-on-the-client-and-the-server*/}
311
311
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
+
Ako [renderujete više nezavisnih React aplikacija na istoj stranici](#specifying-a-shared-prefix-for-all-generated-ids), a neke od tih aplikacija su renderovane na serveru, postarajte se da `identifierPrefix`koji prosleđujete u [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) poziv na klijentskoj strani bude isti kao i `identifierPrefix`koji prosleđujete u [serverske API-je](/reference/react-dom/server) poput [`renderToPipeableStream`](/reference/react-dom/server/renderToPipeableStream).
0 commit comments