Skip to content

Commit 1fa3fbd

Browse files
translate(reference): forwardRef (#693)
Signed-off-by: Alessandro De Blasis <alex@deblasis.net> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 3f3bb3f commit 1fa3fbd

1 file changed

Lines changed: 49 additions & 42 deletions

File tree

src/content/reference/react/forwardRef.md

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
---
22
title: forwardRef
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/forwardRef.md).
9+
10+
</Note>
11+
512
<Deprecated>
613

7-
In React 19, `forwardRef` is no longer necessary. Pass `ref` as a prop instead.
14+
In React 19, `forwardRef` non è più necessario. Passa `ref` come prop.
815

9-
`forwardRef` will be deprecated in a future release. Learn more [here](/blog/2024/04/25/react-19#ref-as-a-prop).
16+
`forwardRef` sarà deprecato in una futura release. Scopri di più [qui](/blog/2024/04/25/react-19#ref-as-a-prop).
1017

1118
</Deprecated>
1219

1320
<Intro>
1421

15-
`forwardRef` lets your component expose a DOM node to the parent component with a [ref.](/learn/manipulating-the-dom-with-refs)
22+
`forwardRef` ti permette di esporre un nodo DOM al componente genitore con un [ref.](/learn/manipulating-the-dom-with-refs)
1623

1724
```js
1825
const SomeComponent = forwardRef(render)
@@ -28,7 +35,7 @@ const SomeComponent = forwardRef(render)
2835

2936
### `forwardRef(render)` {/*forwardref*/}
3037

31-
Call `forwardRef()` to let your component receive a ref and forward it to a child component:
38+
Chiama `forwardRef()` per far sì che il tuo componente riceva un ref e lo inoltri a un componente figlio:
3239

3340
```js
3441
import { forwardRef } from 'react';
@@ -38,26 +45,26 @@ const MyInput = forwardRef(function MyInput(props, ref) {
3845
});
3946
```
4047

41-
[See more examples below.](#usage)
48+
[Vedi altri esempi sotto.](#usage)
4249

4350
#### Parameters {/*parameters*/}
4451

45-
* `render`: The render function for your component. React calls this function with the props and `ref` that your component received from its parent. The JSX you return will be the output of your component.
52+
* `render`: La funzione render del tuo componente. React chiama questa funzione con le props e il `ref` che il tuo componente ha ricevuto dal genitore. Il JSX che restituisci sarà l'output del tuo componente.
4653

4754
#### Returns {/*returns*/}
4855

49-
`forwardRef` returns a React component that you can render in JSX. Unlike React components defined as plain functions, a component returned by `forwardRef` is also able to receive a `ref` prop.
56+
`forwardRef` restituisce un componente React che puoi renderizzare in JSX. A differenza dei componenti React definiti come funzioni semplici, un componente restituito da `forwardRef` può anche ricevere una prop `ref`.
5057

5158
#### Caveats {/*caveats*/}
5259

53-
* In Strict Mode, React will **call your render function twice** in order to [help you find accidental impurities.](/reference/react/useState#my-initializer-or-updater-function-runs-twice) This is development-only behavior and does not affect production. If your render function is pure (as it should be), this should not affect the logic of your component. The result from one of the calls will be ignored.
60+
* In Strict Mode, React **chiamerà la tua funzione render due volte** per [aiutarti a trovare impurità accidentali.](/reference/react/useState#my-initializer-or-updater-function-runs-twice) Questo comportamento vale solo in sviluppo e non influisce sulla produzione. Se la tua funzione render è pura (come dovrebbe essere), non dovrebbe influire sulla logica del tuo componente. Il risultato di una delle chiamate verrà ignorato.
5461

5562

5663
---
5764

58-
### `render` function {/*render-function*/}
65+
### Funzione `render` {/*render-function*/}
5966

60-
`forwardRef` accepts a render function as an argument. React calls this function with `props` and `ref`:
67+
`forwardRef` accetta una funzione render come argomento. React chiama questa funzione con `props` e `ref`:
6168

6269
```js
6370
const MyInput = forwardRef(function MyInput(props, ref) {
@@ -72,21 +79,21 @@ const MyInput = forwardRef(function MyInput(props, ref) {
7279

7380
#### Parameters {/*render-parameters*/}
7481

75-
* `props`: The props passed by the parent component.
82+
* `props`: Le props passate dal componente genitore.
7683

77-
* `ref`: The `ref` attribute passed by the parent component. The `ref` can be an object or a function. If the parent component has not passed a ref, it will be `null`. You should either pass the `ref` you receive to another component, or pass it to [`useImperativeHandle`.](/reference/react/useImperativeHandle)
84+
* `ref`: L'attributo `ref` passato dal componente genitore. Il `ref` può essere un oggetto o una funzione. Se il componente genitore non ha passato un ref, sarà `null`. Dovresti inoltrare il `ref` che ricevi a un altro componente oppure passarlo a [`useImperativeHandle`.](/reference/react/useImperativeHandle)
7885

7986
#### Returns {/*render-returns*/}
8087

81-
`forwardRef` returns a React component that you can render in JSX. Unlike React components defined as plain functions, the component returned by `forwardRef` is able to take a `ref` prop.
88+
`forwardRef` restituisce un componente React che puoi renderizzare in JSX. A differenza dei componenti React definiti come funzioni semplici, il componente restituito da `forwardRef` può ricevere una prop `ref`.
8289

8390
---
8491

8592
## Usage {/*usage*/}
8693

87-
### Exposing a DOM node to the parent component {/*exposing-a-dom-node-to-the-parent-component*/}
94+
### Esporre un nodo DOM al componente genitore {/*exposing-a-dom-node-to-the-parent-component*/}
8895

89-
By default, each component's DOM nodes are private. However, sometimes it's useful to expose a DOM node to the parent--for example, to allow focusing it. To opt in, wrap your component definition into `forwardRef()`:
96+
Per impostazione predefinita, i nodi DOM di ogni componente sono privati. A volte però è utile esporre un nodo DOM al genitore — ad esempio, per metterlo a fuoco. Per attivare questa opzione, avvolgi la definizione del componente in `forwardRef()`:
9097

9198
```js {3,11}
9299
import { forwardRef } from 'react';
@@ -102,7 +109,7 @@ const MyInput = forwardRef(function MyInput(props, ref) {
102109
});
103110
```
104111

105-
You will receive a <CodeStep step={1}>ref</CodeStep> as the second argument after props. Pass it to the DOM node that you want to expose:
112+
Riceverai un <CodeStep step={1}>ref</CodeStep> come secondo argomento dopo le props. Passalo al nodo DOM che vuoi esporre:
106113

107114
```js {8} [[1, 3, "ref"], [1, 8, "ref", 30]]
108115
import { forwardRef } from 'react';
@@ -118,7 +125,7 @@ const MyInput = forwardRef(function MyInput(props, ref) {
118125
});
119126
```
120127

121-
This lets the parent `Form` component access the <CodeStep step={2}>`<input>` DOM node</CodeStep> exposed by `MyInput`:
128+
Questo permette al componente genitore `Form` di accedere al <CodeStep step={2}>nodo DOM `<input>`</CodeStep> esposto da `MyInput`:
122129

123130
```js [[1, 2, "ref"], [1, 10, "ref", 41], [2, 5, "ref.current"]]
124131
function Form() {
@@ -139,15 +146,15 @@ function Form() {
139146
}
140147
```
141148

142-
This `Form` component [passes a ref](/reference/react/useRef#manipulating-the-dom-with-a-ref) to `MyInput`. The `MyInput` component *forwards* that ref to the `<input>` browser tag. As a result, the `Form` component can access that `<input>` DOM node and call [`focus()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) on it.
149+
Questo componente `Form` [passa un ref](/reference/react/useRef#manipulating-the-dom-with-a-ref) a `MyInput`. Il componente `MyInput` *inoltra* quel ref al tag browser `<input>`. Di conseguenza, il componente `Form` può accedere a quel nodo DOM `<input>` e chiamare [`focus()`](https://developer.mozilla.org/it/docs/Web/API/HTMLElement/focus) su di esso.
143150

144-
Keep in mind that exposing a ref to the DOM node inside your component makes it harder to change your component's internals later. You will typically expose DOM nodes from reusable low-level components like buttons or text inputs, but you won't do it for application-level components like an avatar or a comment.
151+
Tieni presente che esporre un ref al nodo DOM interno al tuo componente complica il cambiamento dei dettagli interni in seguito. In genere esporrai nodi DOM da componenti riutilizzabili di basso livello come pulsanti o campi di testo, ma non lo farai per componenti a livello applicativo come un avatar o un commento.
145152

146-
<Recipes titleText="Examples of forwarding a ref">
153+
<Recipes titleText="Esempi di inoltro di un ref">
147154

148-
#### Focusing a text input {/*focusing-a-text-input*/}
155+
#### Mettere a fuoco un campo di testo {/*focusing-a-text-input*/}
149156

150-
Clicking the button will focus the input. The `Form` component defines a ref and passes it to the `MyInput` component. The `MyInput` component forwards that ref to the browser `<input>`. This lets the `Form` component focus the `<input>`.
157+
Cliccando il pulsante metterai a fuoco l'input. Il componente `Form` definisce un ref e lo passa al componente `MyInput`. Il componente `MyInput` inoltra quel ref al `<input>` del browser. Questo permette al componente `Form` di mettere a fuoco l'`<input>`.
151158

152159
<Sandpack>
153160

@@ -199,9 +206,9 @@ input {
199206

200207
<Solution />
201208

202-
#### Playing and pausing a video {/*playing-and-pausing-a-video*/}
209+
#### Riprodurre e mettere in pausa un video {/*playing-and-pausing-a-video*/}
203210

204-
Clicking the button will call [`play()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/play) and [`pause()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/pause) on a `<video>` DOM node. The `App` component defines a ref and passes it to the `MyVideoPlayer` component. The `MyVideoPlayer` component forwards that ref to the browser `<video>` node. This lets the `App` component play and pause the `<video>`.
211+
Cliccando il pulsante chiamerai [`play()`](https://developer.mozilla.org/it/docs/Web/API/HTMLMediaElement/play) e [`pause()`](https://developer.mozilla.org/it/docs/Web/API/HTMLMediaElement/pause) su un nodo DOM `<video>`. Il componente `App` definisce un ref e lo passa al componente `MyVideoPlayer`. Il componente `MyVideoPlayer` inoltra quel ref al nodo browser `<video>`. Questo permette al componente `App` di riprodurre e mettere in pausa il `<video>`.
205212

206213
<Sandpack>
207214

@@ -260,9 +267,9 @@ button { margin-bottom: 10px; margin-right: 10px; }
260267

261268
---
262269

263-
### Forwarding a ref through multiple components {/*forwarding-a-ref-through-multiple-components*/}
270+
### Inoltrare un ref attraverso più componenti {/*forwarding-a-ref-through-multiple-components*/}
264271

265-
Instead of forwarding a `ref` to a DOM node, you can forward it to your own component like `MyInput`:
272+
Invece di inoltrare un `ref` a un nodo DOM, puoi inoltrarlo al tuo componente, come `MyInput`:
266273

267274
```js {1,5}
268275
const FormField = forwardRef(function FormField(props, ref) {
@@ -276,7 +283,7 @@ const FormField = forwardRef(function FormField(props, ref) {
276283
});
277284
```
278285

279-
If that `MyInput` component forwards a ref to its `<input>`, a ref to `FormField` will give you that `<input>`:
286+
Se quel componente `MyInput` inoltra un ref al suo `<input>`, un ref a `FormField` ti darà accesso a quell'`<input>`:
280287

281288
```js {2,5,10}
282289
function Form() {
@@ -297,7 +304,7 @@ function Form() {
297304
}
298305
```
299306

300-
The `Form` component defines a ref and passes it to `FormField`. The `FormField` component forwards that ref to `MyInput`, which forwards it to a browser `<input>` DOM node. This is how `Form` accesses that DOM node.
307+
Il componente `Form` definisce un ref e lo passa a `FormField`. Il componente `FormField` inoltra quel ref a `MyInput`, che lo inoltra a un nodo DOM `<input>` del browser. È così che `Form` accede a quel nodo DOM.
301308

302309

303310
<Sandpack>
@@ -375,9 +382,9 @@ input, button {
375382

376383
---
377384

378-
### Exposing an imperative handle instead of a DOM node {/*exposing-an-imperative-handle-instead-of-a-dom-node*/}
385+
### Esporre un handle imperativo invece di un nodo DOM {/*exposing-an-imperative-handle-instead-of-a-dom-node*/}
379386

380-
Instead of exposing an entire DOM node, you can expose a custom object, called an *imperative handle,* with a more constrained set of methods. To do this, you'd need to define a separate ref to hold the DOM node:
387+
Invece di esporre un intero nodo DOM, puoi esporre un oggetto personalizzato, chiamato *handle imperativo,* con un insieme più ristretto di metodi. Per farlo, devi definire un ref separato per contenere il nodo DOM:
381388

382389
```js {2,6}
383390
const MyInput = forwardRef(function MyInput(props, ref) {
@@ -389,7 +396,7 @@ const MyInput = forwardRef(function MyInput(props, ref) {
389396
});
390397
```
391398

392-
Pass the `ref` you received to [`useImperativeHandle`](/reference/react/useImperativeHandle) and specify the value you want to expose to the `ref`:
399+
Passa il `ref` che hai ricevuto a [`useImperativeHandle`](/reference/react/useImperativeHandle) e specifica il valore che vuoi esporre al `ref`:
393400

394401
```js {6-15}
395402
import { forwardRef, useRef, useImperativeHandle } from 'react';
@@ -412,7 +419,7 @@ const MyInput = forwardRef(function MyInput(props, ref) {
412419
});
413420
```
414421

415-
If some component gets a ref to `MyInput`, it will only receive your `{ focus, scrollIntoView }` object instead of the DOM node. This lets you limit the information you expose about your DOM node to the minimum.
422+
Se un componente ottiene un ref a `MyInput`, riceverà solo il tuo oggetto `{ focus, scrollIntoView }` invece del nodo DOM. Questo ti permette di limitare al minimo le informazioni che esponi sul tuo nodo DOM.
416423

417424
<Sandpack>
418425

@@ -425,7 +432,7 @@ export default function Form() {
425432

426433
function handleClick() {
427434
ref.current.focus();
428-
// This won't work because the DOM node isn't exposed:
435+
// Non funzionerà perché il nodo DOM non è esposto:
429436
// ref.current.style.opacity = 0.5;
430437
}
431438

@@ -471,25 +478,25 @@ input {
471478

472479
</Sandpack>
473480

474-
[Read more about using imperative handles.](/reference/react/useImperativeHandle)
481+
[Leggi di più sull'uso degli handle imperativi.](/reference/react/useImperativeHandle)
475482

476483
<Pitfall>
477484

478-
**Do not overuse refs.** You should only use refs for *imperative* behaviors that you can't express as props: for example, scrolling to a node, focusing a node, triggering an animation, selecting text, and so on.
485+
**Non abusare dei ref.** Dovresti usare i ref solo per comportamenti *imperativi* che non puoi esprimere come props: ad esempio, scorrere fino a un nodo, mettere a fuoco un nodo, avviare un'animazione, selezionare del testo e così via.
479486

480-
**If you can express something as a prop, you should not use a ref.** For example, instead of exposing an imperative handle like `{ open, close }` from a `Modal` component, it is better to take `isOpen` as a prop like `<Modal isOpen={isOpen} />`. [Effects](/learn/synchronizing-with-effects) can help you expose imperative behaviors via props.
487+
**Se puoi esprimere qualcosa come prop, non dovresti usare un ref.** Ad esempio, invece di esporre un handle imperativo come `{ open, close }` da un componente `Modal`, è meglio accettare `isOpen` come prop, come `<Modal isOpen={isOpen} />`. Gli [Effetti](/learn/synchronizing-with-effects) possono aiutarti a esporre comportamenti imperativi tramite props.
481488

482489
</Pitfall>
483490

484491
---
485492

486493
## Troubleshooting {/*troubleshooting*/}
487494

488-
### My component is wrapped in `forwardRef`, but the `ref` to it is always `null` {/*my-component-is-wrapped-in-forwardref-but-the-ref-to-it-is-always-null*/}
495+
### Il mio componente è avvolto in `forwardRef`, ma il `ref` verso di esso è sempre `null` {/*my-component-is-wrapped-in-forwardref-but-the-ref-to-it-is-always-null*/}
489496

490-
This usually means that you forgot to actually use the `ref` that you received.
497+
Di solito significa che hai dimenticato di usare effettivamente il `ref` che hai ricevuto.
491498

492-
For example, this component doesn't do anything with its `ref`:
499+
Ad esempio, questo componente non fa nulla con il suo `ref`:
493500

494501
```js {1}
495502
const MyInput = forwardRef(function MyInput({ label }, ref) {
@@ -502,7 +509,7 @@ const MyInput = forwardRef(function MyInput({ label }, ref) {
502509
});
503510
```
504511

505-
To fix it, pass the `ref` down to a DOM node or another component that can accept a ref:
512+
Per risolvere, passa il `ref` a un nodo DOM o a un altro componente che può accettare un ref:
506513

507514
```js {1,5}
508515
const MyInput = forwardRef(function MyInput({ label }, ref) {
@@ -515,7 +522,7 @@ const MyInput = forwardRef(function MyInput({ label }, ref) {
515522
});
516523
```
517524

518-
The `ref` to `MyInput` could also be `null` if some of the logic is conditional:
525+
Il `ref` a `MyInput` potrebbe anche essere `null` se parte della logica è condizionale:
519526

520527
```js {1,5}
521528
const MyInput = forwardRef(function MyInput({ label, showInput }, ref) {
@@ -528,7 +535,7 @@ const MyInput = forwardRef(function MyInput({ label, showInput }, ref) {
528535
});
529536
```
530537

531-
If `showInput` is `false`, then the ref won't be forwarded to any node, and a ref to `MyInput` will remain empty. This is particularly easy to miss if the condition is hidden inside another component, like `Panel` in this example:
538+
Se `showInput` è `false`, il ref non verrà inoltrato a nessun nodo e un ref a `MyInput` resterà vuoto. È particolarmente facile non accorgersene se la condizione è nascosta dentro un altro componente, come `Panel` in questo esempio:
532539

533540
```js {5,7}
534541
const MyInput = forwardRef(function MyInput({ label, showInput }, ref) {

0 commit comments

Comments
 (0)