Skip to content

Commit 5306f17

Browse files
committed
translate(reference): useImperativeHandle
Signed-off-by: Alessandro De Blasis <alex@deblasis.net>
1 parent ba6423a commit 5306f17

1 file changed

Lines changed: 28 additions & 21 deletions

File tree

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

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
---
22
title: useImperativeHandle
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/useImperativeHandle.md).
9+
10+
</Note>
11+
512
<Intro>
613

7-
`useImperativeHandle` is a React Hook that lets you customize the handle exposed as a [ref.](/learn/manipulating-the-dom-with-refs)
14+
`useImperativeHandle` è un Hook React che ti permette di personalizzare l'handle esposto come [ref.](/learn/manipulating-the-dom-with-refs)
815

916
```js
1017
useImperativeHandle(ref, createHandle, dependencies?)
@@ -20,73 +27,73 @@ useImperativeHandle(ref, createHandle, dependencies?)
2027
2128
### `useImperativeHandle(ref, createHandle, dependencies?)` {/*useimperativehandle*/}
2229
23-
Call `useImperativeHandle` at the top level of your component to customize the ref handle it exposes:
30+
Chiama `useImperativeHandle` al top level del tuo componente per personalizzare l'handle del ref che espone:
2431
2532
```js
2633
import { useImperativeHandle } from 'react';
2734

2835
function MyInput({ ref }) {
2936
useImperativeHandle(ref, () => {
3037
return {
31-
// ... your methods ...
38+
// ... i tuoi metodi ...
3239
};
3340
}, []);
3441
// ...
3542
```
3643
37-
[See more examples below.](#usage)
44+
[Vedi altri esempi sotto.](#usage)
3845
3946
#### Parameters {/*parameters*/}
4047
41-
* `ref`: The `ref` you received as a prop to the `MyInput` component.
48+
* `ref`: La `ref` che hai ricevuto come prop del componente `MyInput`.
4249
43-
* `createHandle`: A function that takes no arguments and returns the ref handle you want to expose. That ref handle can have any type. Usually, you will return an object with the methods you want to expose.
50+
* `createHandle`: Una funzione che non accetta argomenti e restituisce l'handle del ref che vuoi esporre. L'handle del ref può essere di qualsiasi tipo. Di solito, restituirai un oggetto con i metodi che vuoi esporre.
4451
45-
* **optional** `dependencies`: The list of all reactive values referenced inside of the `createHandle` code. Reactive values include props, state, and all the variables and functions declared directly inside your component body. If your linter is [configured for React](/learn/editor-setup#linting), it will verify that every reactive value is correctly specified as a dependency. The list of dependencies must have a constant number of items and be written inline like `[dep1, dep2, dep3]`. React will compare each dependency with its previous value using the [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) comparison. If a re-render resulted in a change to some dependency, or if you omitted this argument, your `createHandle` function will re-execute, and the newly created handle will be assigned to the ref.
52+
* **optional** `dependencies`: L'elenco di tutti i valori reattivi referenziati all'interno del codice di `createHandle`. I valori reattivi includono props, state e tutte le variabili e funzioni dichiarate direttamente nel corpo del componente. Se il tuo linter è [configurato per React](/learn/editor-setup#linting), verificherà che ogni valore reattivo sia specificato correttamente come dipendenza. L'elenco delle dipendenze deve avere un numero costante di elementi ed essere scritto inline come `[dep1, dep2, dep3]`. React confronterà ogni dipendenza con il suo valore precedente usando il confronto [`Object.is`](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Object/is). Se una ri-renderizzazione ha comportato un cambiamento in qualche dipendenza, o se hai omesso questo argomento, la tua funzione `createHandle` verrà rieseguita e l'handle appena creato verrà assegnato alla ref.
4653
4754
<Note>
4855
49-
Starting with React 19, [`ref` is available as a prop.](/blog/2024/12/05/react-19#ref-as-a-prop) In React 18 and earlier, it was necessary to get the `ref` from [`forwardRef`.](/reference/react/forwardRef)
56+
A partire da React 19, [`ref` è disponibile come prop.](/blog/2024/12/05/react-19#ref-as-a-prop) In React 18 e versioni precedenti, era necessario ottenere la `ref` da [`forwardRef`.](/reference/react/forwardRef)
5057
5158
</Note>
5259
5360
#### Returns {/*returns*/}
5461
55-
`useImperativeHandle` returns `undefined`.
62+
`useImperativeHandle` restituisce `undefined`.
5663
5764
---
5865
5966
## Usage {/*usage*/}
6067
61-
### Exposing a custom ref handle to the parent component {/*exposing-a-custom-ref-handle-to-the-parent-component*/}
68+
### Esporre un handle ref personalizzato al componente genitore {/*exposing-a-custom-ref-handle-to-the-parent-component*/}
6269
63-
To expose a DOM node to the parent element, pass in the `ref` prop to the node.
70+
Per esporre un nodo DOM al componente genitore, passa la prop `ref` al nodo.
6471
6572
```js {2}
6673
function MyInput({ ref }) {
6774
return <input ref={ref} />;
6875
};
6976
```
7077
71-
With the code above, [a ref to `MyInput` will receive the `<input>` DOM node.](/learn/manipulating-the-dom-with-refs) However, you can expose a custom value instead. To customize the exposed handle, call `useImperativeHandle` at the top level of your component:
78+
Con il codice sopra, [una ref a `MyInput` riceverà il nodo DOM `<input>`.](/learn/manipulating-the-dom-with-refs) Tuttavia, puoi esporre un valore personalizzato al suo posto. Per personalizzare l'handle esposto, chiama `useImperativeHandle` al top level del tuo componente:
7279
7380
```js {4-8}
7481
import { useImperativeHandle } from 'react';
7582

7683
function MyInput({ ref }) {
7784
useImperativeHandle(ref, () => {
7885
return {
79-
// ... your methods ...
86+
// ... i tuoi metodi ...
8087
};
8188
}, []);
8289

8390
return <input />;
8491
};
8592
```
8693
87-
Note that in the code above, the `ref` is no longer passed to the `<input>`.
94+
Nota che nel codice sopra, la `ref` non viene più passata all'`<input>`.
8895
89-
For example, suppose you don't want to expose the entire `<input>` DOM node, but you want to expose two of its methods: `focus` and `scrollIntoView`. To do this, keep the real browser DOM in a separate ref. Then use `useImperativeHandle` to expose a handle with only the methods that you want the parent component to call:
96+
Ad esempio, supponiamo che tu non voglia esporre l'intero nodo DOM `<input>`, ma che tu voglia esporre due dei suoi metodi: `focus` e `scrollIntoView`. Per farlo, mantieni il DOM del browser reale in una ref separata. Poi usa `useImperativeHandle` per esporre un handle con solo i metodi che vuoi che il componente genitore chiami:
9097
9198
```js {7-14}
9299
import { useRef, useImperativeHandle } from 'react';
@@ -109,7 +116,7 @@ function MyInput({ ref }) {
109116
};
110117
```
111118
112-
Now, if the parent component gets a ref to `MyInput`, it will be able to call the `focus` and `scrollIntoView` methods on it. However, it will not have full access to the underlying `<input>` DOM node.
119+
Ora, se il componente genitore ottiene una ref a `MyInput`, sarà in grado di chiamare i metodi `focus` e `scrollIntoView` su di esso. Tuttavia, non avrà pieno accesso al nodo DOM `<input>` sottostante.
113120
114121
<Sandpack>
115122
@@ -122,7 +129,7 @@ export default function Form() {
122129

123130
function handleClick() {
124131
ref.current.focus();
125-
// This won't work because the DOM node isn't exposed:
132+
// Non funzionerà perché il nodo DOM non è esposto:
126133
// ref.current.style.opacity = 0.5;
127134
}
128135

@@ -170,9 +177,9 @@ input {
170177
171178
---
172179
173-
### Exposing your own imperative methods {/*exposing-your-own-imperative-methods*/}
180+
### Esporre i tuoi metodi imperativi {/*exposing-your-own-imperative-methods*/}
174181
175-
The methods you expose via an imperative handle don't have to match the DOM methods exactly. For example, this `Post` component exposes a `scrollAndFocusAddComment` method via an imperative handle. This lets the parent `Page` scroll the list of comments *and* focus the input field when you click the button:
182+
I metodi che esponi tramite un handle imperativo non devono corrispondere esattamente ai metodi del DOM. Ad esempio, questo componente `Post` espone un metodo `scrollAndFocusAddComment` tramite un handle imperativo. Questo permette al componente genitore `Page` di scorrere l'elenco dei commenti *e* mettere il focus sul campo di input quando clicchi su un pulsante:
176183
177184
<Sandpack>
178185
@@ -285,8 +292,8 @@ export default AddComment;
285292
286293
<Pitfall>
287294
288-
**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.
295+
**Non abusare delle ref.** Dovresti usare le ref solo per comportamenti *imperativi* che non puoi esprimere come props: ad esempio, scorrere fino a un nodo, mettere il focus su un nodo, attivare un'animazione, selezionare del testo, e così via.
289296
290-
**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.
297+
**Se puoi esprimere qualcosa come prop, non dovresti usare una 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.
291298
292299
</Pitfall>

0 commit comments

Comments
 (0)