Skip to content

Commit 569bded

Browse files
committed
translate(reference): useId
Signed-off-by: Alessandro De Blasis <alex@deblasis.net>
1 parent df4d110 commit 569bded

1 file changed

Lines changed: 50 additions & 43 deletions

File tree

‎src/content/reference/react/useId.md‎

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
---
22
title: useId
3+
translationStatus: ai-draft
34
---
45

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+
512
<Intro>
613

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à.
815

916
```js
1017
const id = useId()
@@ -20,7 +27,7 @@ const id = useId()
2027

2128
### `useId()` {/*useid*/}
2229

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:
2431

2532
```js
2633
import { useId } from 'react';
@@ -30,39 +37,39 @@ function PasswordField() {
3037
// ...
3138
```
3239
33-
[See more examples below.](#usage)
40+
[Vedi altri esempi sotto.](#usage)
3441
3542
#### Parameters {/*parameters*/}
3643
37-
`useId` does not take any parameters.
44+
`useId` non accetta parametri.
3845
3946
#### Returns {/*returns*/}
4047
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.
4249
4350
#### Caveats {/*caveats*/}
4451
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 Hooks 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.
4653
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.
4855
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)
5057
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 nei [Server Component asincroni.](/reference/rsc/server-components#async-components-with-server-components)
5259
5360
---
5461
5562
## Usage {/*usage*/}
5663
5764
<Pitfall>
5865
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)
6067
6168
</Pitfall>
6269
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*/}
6471
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:
6673
6774
```js [[1, 4, "passwordHintId"]]
6875
import { useId } from 'react';
@@ -72,7 +79,7 @@ function PasswordField() {
7279
// ...
7380
```
7481
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:
7683
7784
```js [[1, 2, "passwordHintId"], [1, 3, "passwordHintId"]]
7885
<>
@@ -81,11 +88,11 @@ You can then pass the <CodeStep step={1}>generated ID</CodeStep> to different at
8188
</>
8289
```
8390
84-
**Let's walk through an example to see when this is useful.**
91+
**Vediamo un esempio per capire quando è utile.**
8592
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).
8794
88-
In regular HTML, you would write it like this:
95+
In HTML classico, lo scriveresti così:
8996
9097
```html {5,8}
9198
<label>
@@ -100,7 +107,7 @@ In regular HTML, you would write it like this:
100107
</p>
101108
```
102109
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`:
104111
105112
```js {4,11,14}
106113
import { useId } from 'react';
@@ -117,14 +124,14 @@ function PasswordField() {
117124
/>
118125
</label>
119126
<p id={passwordHintId}>
120-
The password should contain at least 18 characters
127+
La password deve contenere almeno 18 caratteri
121128
</p>
122129
</>
123130
);
124131
}
125132
```
126133
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.
128135
129136
<Sandpack>
130137
@@ -143,7 +150,7 @@ function PasswordField() {
143150
/>
144151
</label>
145152
<p id={passwordHintId}>
146-
The password should contain at least 18 characters
153+
La password deve contenere almeno 18 caratteri
147154
</p>
148155
</>
149156
);
@@ -152,9 +159,9 @@ function PasswordField() {
152159
export default function App() {
153160
return (
154161
<>
155-
<h2>Choose password</h2>
162+
<h2>Scegli password</h2>
156163
<PasswordField />
157-
<h2>Confirm password</h2>
164+
<h2>Conferma password</h2>
158165
<PasswordField />
159166
</>
160167
);
@@ -167,33 +174,33 @@ input { margin: 5px; }
167174
168175
</Sandpack>
169176
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.
171178
172179
<Pitfall>
173180
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 il [rendering 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.
175182
176183
</Pitfall>
177184
178185
<DeepDive>
179186
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*/}
181188
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++`.
183190
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 il [rendering lato server.](/reference/react-dom/server) Durante il rendering 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.
185192
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.
187194
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.
189196
190197
</DeepDive>
191198
192199
---
193200
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*/}
195202
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:
197204
198205
<Sandpack>
199206
@@ -204,10 +211,10 @@ export default function Form() {
204211
const id = useId();
205212
return (
206213
<form>
207-
<label htmlFor={id + '-firstName'}>First Name:</label>
214+
<label htmlFor={id + '-firstName'}>Nome:</label>
208215
<input id={id + '-firstName'} type="text" />
209216
<hr />
210-
<label htmlFor={id + '-lastName'}>Last Name:</label>
217+
<label htmlFor={id + '-lastName'}>Cognome:</label>
211218
<input id={id + '-lastName'} type="text" />
212219
</form>
213220
);
@@ -220,20 +227,20 @@ input { margin: 5px; }
220227
221228
</Sandpack>
222229
223-
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.
224231
225232
---
226233
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*/}
228235
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.
230237
231238
<Sandpack>
232239
233240
```html public/index.html
234241
<!DOCTYPE html>
235242
<html>
236-
<head><title>My app</title></head>
243+
<head><title>La mia app</title></head>
237244
<body>
238245
<div id="root1"></div>
239246
<div id="root2"></div>
@@ -246,7 +253,7 @@ import { useId } from 'react';
246253

247254
function PasswordField() {
248255
const passwordHintId = useId();
249-
console.log('Generated identifier:', passwordHintId)
256+
console.log('Identificatore generato:', passwordHintId)
250257
return (
251258
<>
252259
<label>
@@ -257,7 +264,7 @@ function PasswordField() {
257264
/>
258265
</label>
259266
<p id={passwordHintId}>
260-
The password should contain at least 18 characters
267+
La password deve contenere almeno 18 caratteri
261268
</p>
262269
</>
263270
);
@@ -266,7 +273,7 @@ function PasswordField() {
266273
export default function App() {
267274
return (
268275
<>
269-
<h2>Choose password</h2>
276+
<h2>Scegli password</h2>
270277
<PasswordField />
271278
</>
272279
);
@@ -309,9 +316,9 @@ input { margin: 5px; }
309316
310317
---
311318
312-
### 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*/}
313320
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)
315322
316323
```js
317324
// Server
@@ -335,4 +342,4 @@ const root = hydrateRoot(
335342
);
336343
```
337344
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

Comments
 (0)