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-44Lines changed: 51 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,17 @@
1
1
---
2
2
title: useId
3
+
translationStatus: ai-draft
3
4
---
4
5
6
+
<Note>
7
+
8
+
Questa pagina è stata tradotta automaticamente e supervisionata da un maintainer. Un'ulteriore revisione da parte della community sarebbe comunque utile. [Migliora questa traduzione](https://github.com/reactjs/it.react.dev/edit/main/src/content/reference/react/useId.md).
9
+
10
+
</Note>
11
+
5
12
<Intro>
6
13
7
-
`useId`is a React Hook for generating unique IDs that can be passed to accessibility attributes.
14
+
`useId`è un Hook React per generare ID univoci che possono essere passati agli attributi di accessibilità.
8
15
9
16
```js
10
17
constid=useId()
@@ -20,7 +27,7 @@ const id = useId()
20
27
21
28
### `useId()` {/*useid*/}
22
29
23
-
Call`useId`at the top level of your component to generate a unique ID:
30
+
Chiama`useId`al top level del tuo componente per generare un ID univoco:
24
31
25
32
```js
26
33
import { useId } from'react';
@@ -30,39 +37,39 @@ function PasswordField() {
30
37
// ...
31
38
```
32
39
33
-
[See more examples below.](#usage)
40
+
[Vedi altri esempi sotto.](#usage)
34
41
35
42
#### Parameters {/*parameters*/}
36
43
37
-
`useId`does not take any parameters.
44
+
`useId`non accetta parametri.
38
45
39
46
#### Returns {/*returns*/}
40
47
41
-
`useId`returns a unique ID string associated with this particular `useId`call in this particular component.
48
+
`useId`restituisce una stringa ID univoca associata a questa particolare chiamata a `useId` in questo particolare componente.
42
49
43
50
#### Caveats {/*caveats*/}
44
51
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.
52
+
* `useId`è un Hook, quindi puoi chiamarlo **solo al top level del tuo componente** o dei tuoi Hook personalizzati. Non puoi chiamarlo all'interno di loop o condizioni. Se ne hai bisogno, estrai un nuovo componente e sposta lo state al suo interno.
46
53
47
-
* `useId` **should not be used to generate cache keys** for [use()](/reference/react/use). The ID is stable when a component is mounted but may change during rendering. Cache keys should be generated from your data.
54
+
* `useId` **non dovrebbe essere usato per generare chiavi di cache** per [use()](/reference/react/use). L'ID è stabile quando un componente è montato, ma potrebbe cambiare durante la renderizzazione. Le chiavi di cache dovrebbero essere generate dai tuoi dati.
48
55
49
-
* `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)
56
+
* `useId` **non dovrebbe essere usato per generare le key** in una lista. [Le key dovrebbero essere generate dai tuoi dati.](/learn/rendering-lists#where-to-get-your-key)
50
57
51
-
* `useId`currently cannot be used in [async Server Components](/reference/rsc/server-components#async-components-with-server-components).
58
+
* `useId`al momento non può essere usato negli [async Server Components](/reference/rsc/server-components#async-components-with-server-components).
52
59
53
60
---
54
61
55
62
## Usage {/*usage*/}
56
63
57
64
<Pitfall>
58
65
59
-
**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)
66
+
**Non chiamare `useId`per generare le key in una lista.** [Le key dovrebbero essere generate dai tuoi dati.](/learn/rendering-lists#where-to-get-your-key)
60
67
61
68
</Pitfall>
62
69
63
-
### Generating unique IDs for accessibility attributes {/*generating-unique-ids-for-accessibility-attributes*/}
70
+
### Generare ID univoci per attributi di accessibilità {/*generating-unique-ids-for-accessibility-attributes*/}
64
71
65
-
Call`useId`at the top level of your component to generate a unique ID:
72
+
Chiama`useId`al top level del tuo componente per generare un ID univoco:
66
73
67
74
```js [[1, 4, "passwordHintId"]]
68
75
import { useId } from'react';
@@ -72,7 +79,7 @@ function PasswordField() {
72
79
// ...
73
80
```
74
81
75
-
You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different attributes:
82
+
Puoi poi passare l'<CodeStep step={1}>ID generato</CodeStep> a diversi attributi:
@@ -81,11 +88,11 @@ You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different at
81
88
</>
82
89
```
83
90
84
-
**Let's walk through an example to see when this is useful.**
91
+
**Vediamo un esempio per capire quando è utile.**
85
92
86
-
[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).
93
+
Gli [attributi di accessibilità HTML](https://developer.mozilla.org/it/docs/Web/Accessibility/ARIA) come [`aria-describedby`](https://developer.mozilla.org/it/docs/Web/Accessibility/ARIA/Attributes/aria-describedby) ti permettono di specificare che due tag sono collegati tra loro. Per esempio, puoi indicare che un elemento (come un input) è descritto da un altro elemento (come un paragrafo).
87
94
88
-
In regular HTML, you would write it like this:
95
+
In HTML classico, lo scriveresti così:
89
96
90
97
```html {5,8}
91
98
<label>
@@ -96,11 +103,11 @@ In regular HTML, you would write it like this:
96
103
/>
97
104
</label>
98
105
<p id="password-hint">
99
-
The password should contain at least 18characters
106
+
La password deve contenere almeno 18caratteri
100
107
</p>
101
108
```
102
109
103
-
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`:
110
+
Tuttavia, hardcodare ID in questo modo non è una buona pratica in React. Un componente può essere renderizzato più volte nella pagina — ma gli ID devono essere univoci! Invece di hardcodare un ID, genera un ID univoco con`useId`:
104
111
105
112
```js {4,11,14}
106
113
import { useId } from'react';
@@ -117,14 +124,14 @@ function PasswordField() {
117
124
/>
118
125
</label>
119
126
<p id={passwordHintId}>
120
-
The password should contain at least 18characters
127
+
La password deve contenere almeno 18caratteri
121
128
</p>
122
129
</>
123
130
);
124
131
}
125
132
```
126
133
127
-
Now, even if`PasswordField`appears multiple times on the screen, the generated IDs won't clash.
134
+
Ora, anche se`PasswordField`compare più volte sullo schermo, gli ID generati non entreranno in conflitto.
128
135
129
136
<Sandpack>
130
137
@@ -143,7 +150,7 @@ function PasswordField() {
143
150
/>
144
151
</label>
145
152
<p id={passwordHintId}>
146
-
The password should contain at least 18characters
153
+
La password deve contenere almeno 18caratteri
147
154
</p>
148
155
</>
149
156
);
@@ -152,9 +159,9 @@ function PasswordField() {
152
159
exportdefaultfunctionApp() {
153
160
return (
154
161
<>
155
-
<h2>Choose password</h2>
162
+
<h2>Scegli password</h2>
156
163
<PasswordField />
157
-
<h2>Confirm password</h2>
164
+
<h2>Conferma password</h2>
158
165
<PasswordField />
159
166
</>
160
167
);
@@ -167,33 +174,33 @@ input { margin: 5px; }
167
174
168
175
</Sandpack>
169
176
170
-
[Watch this video](https://www.youtube.com/watch?v=0dNzNcuEuOo) to see the difference in the user experience with assistive technologies.
177
+
[Guarda questo video](https://www.youtube.com/watch?v=0dNzNcuEuOo) per vedere la differenza nell'esperienza utente con le tecnologie assistive.
171
178
172
179
<Pitfall>
173
180
174
-
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.
181
+
Con la [renderizzazione lato server](/reference/react-dom/server) **`useId`richiede un albero di componenti identico sul server e sul client**. Se gli alberi che renderizzi sul server e sul client non corrispondono esattamente, gli ID generati non corrisponderanno.
175
182
176
183
</Pitfall>
177
184
178
185
<DeepDive>
179
186
180
-
#### Why is useId better than an incrementing counter? {/*why-is-useid-better-than-an-incrementing-counter*/}
187
+
#### Perché useId è migliore di un contatore incrementale? {/*why-is-useid-better-than-an-incrementing-counter*/}
181
188
182
-
You might be wondering why `useId`is better than incrementing a global variable like`nextId++`.
189
+
Potresti chiederti perché `useId`è migliore di incrementare una variabile globale come`nextId++`.
183
190
184
-
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.
191
+
Il vantaggio principale di`useId`è che React garantisce che funzioni con la [renderizzazione lato server](/reference/react-dom/server). Durante la renderizzazione lato server, i tuoi componenti generano output HTML. In seguito, sul client, l'[hydration](/reference/react-dom/client/hydrateRoot) collega i tuoi gestori di eventi all'HTML generato. Affinché l'hydration funzioni, l'output del client deve corrispondere all'HTML del server.
185
192
186
-
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.
193
+
È molto difficile garantirlo con un contatore incrementale, perché l'ordine in cui i Client Component vengono idratati potrebbe non corrispondere all'ordine in cui l'HTML del server è stato emesso. Chiamando `useId`, ti assicuri che l'hydration funzioni e che l'output corrisponda tra server e client.
187
194
188
-
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.
195
+
All'interno di React, `useId`è generato dal "parent path" del componente chiamante. Ecco perché, se l'albero del client e quello del server sono uguali, il "parent path" corrisponderà indipendentemente dall'ordine di renderizzazione.
189
196
190
197
</DeepDive>
191
198
192
199
---
193
200
194
-
### Generating IDs for several related elements {/*generating-ids-for-several-related-elements*/}
201
+
### Generare ID per più elementi correlati {/*generating-ids-for-several-related-elements*/}
195
202
196
-
If you need to give IDs to multiple related elements, you can call `useId`to generate a shared prefix for them:
203
+
Se devi assegnare ID a più elementi correlati, puoi chiamare `useId`per generare un prefisso condiviso per loro:
197
204
198
205
<Sandpack>
199
206
@@ -204,10 +211,10 @@ export default function Form() {
This lets you avoid calling `useId`for every single element that needs a unique ID.
230
+
Questo ti permette di evitare di chiamare `useId`per ogni singolo elemento che ha bisogno di un ID univoco.
224
231
225
232
---
226
233
227
-
### Specifying a shared prefix for all generated IDs {/*specifying-a-shared-prefix-for-all-generated-ids*/}
234
+
### Specificare un prefisso condiviso per tutti gli ID generati {/*specifying-a-shared-prefix-for-all-generated-ids*/}
228
235
229
-
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.
236
+
Se renderizzi più applicazioni React indipendenti su una singola pagina, passa`identifierPrefix`come opzione alle tue chiamate a [`createRoot`](/reference/react-dom/client/createRoot#parameters) o [`hydrateRoot`](/reference/react-dom/client/hydrateRoot). Questo garantisce che gli ID generati dalle due app diverse non entrino mai in conflitto, perché ogni identificatore generato con `useId`inizierà con il prefisso distinto che hai specificato.
230
237
231
238
<Sandpack>
232
239
233
240
```html public/index.html
234
241
<!DOCTYPE html>
235
242
<html>
236
-
<head><title>My app</title></head>
243
+
<head><title>La mia app</title></head>
237
244
<body>
238
245
<div id="root1"></div>
239
246
<div id="root2"></div>
@@ -246,7 +253,7 @@ 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*/}
319
+
### Usare lo stesso prefisso ID sul client e sul server {/*using-the-same-id-prefix-on-the-client-and-the-server*/}
313
320
314
-
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)
321
+
Se [renderizzi più applicazioni React indipendenti sulla stessa pagina](#specifying-a-shared-prefix-for-all-generated-ids) e alcune di queste app sono renderizzate lato server, assicurati che l'`identifierPrefix`che passi alla chiamata a [`hydrateRoot`](/reference/react-dom/client/hydrateRoot) sul client sia lo stesso `identifierPrefix`che passi alle [API del server](/reference/react-dom/server) come [`renderToPipeableStream`](/reference/react-dom/server/renderToPipeableStream).
315
322
316
323
```js
317
324
// Server
@@ -335,4 +342,4 @@ const root = hydrateRoot(
335
342
);
336
343
```
337
344
338
-
You do not need to pass `identifierPrefix`if you only have one React app on the page.
345
+
Non devi passare `identifierPrefix`se hai solo un'app React sulla pagina.
0 commit comments