From 008863957cf910141ae6464639c9ab9e5e160235 Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 14 Sep 2026 08:12:17 +0300 Subject: [PATCH 1/2] translate(learn): manipulating the DOM with refs Signed-off-by: Alessandro De Blasis Co-authored-by: Cursor --- .../learn/manipulating-the-dom-with-refs.md | 179 +++++++++--------- src/sidebarLearn.json | 2 +- 2 files changed, 94 insertions(+), 87 deletions(-) diff --git a/src/content/learn/manipulating-the-dom-with-refs.md b/src/content/learn/manipulating-the-dom-with-refs.md index 2d1ee8685..d02545546 100644 --- a/src/content/learn/manipulating-the-dom-with-refs.md +++ b/src/content/learn/manipulating-the-dom-with-refs.md @@ -1,52 +1,59 @@ --- -title: 'Manipulating the DOM with Refs' +title: Manipolare il DOM con i ref +translationStatus: ai-draft --- + + +Questa pagina è stata tradotta automaticamente e potrebbe beneficiare di una revisione umana. [Migliora questa traduzione](https://github.com/reactjs/it.react.dev/edit/main/src/content/learn/manipulating-the-dom-with-refs.md). + + + -React automatically updates the [DOM](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) to match your render output, so your components won't often need to manipulate it. However, sometimes you might need access to the DOM elements managed by React--for example, to focus a node, scroll to it, or measure its size and position. There is no built-in way to do those things in React, so you will need a *ref* to the DOM node. +React aggiorna automaticamente il [DOM](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) in modo che corrisponda all'output della renderizzazione, quindi i tuoi componenti non dovranno spesso manipolarlo. Tuttavia, a volte potresti aver bisogno di accedere agli elementi DOM gestiti da React — ad esempio, per mettere a fuoco un nodo, scorrere fino a esso o misurarne dimensione e posizione. Non esiste un modo integrato per fare queste cose in React, quindi avrai bisogno di un *ref* al nodo DOM. -- How to access a DOM node managed by React with the `ref` attribute -- How the `ref` JSX attribute relates to the `useRef` Hook -- How to access another component's DOM node -- In which cases it's safe to modify the DOM managed by React +- Come accedere a un nodo DOM gestito da React con l'attributo `ref` +- Come l'attributo JSX `ref` si collega all'Hook `useRef` +- Come accedere al nodo DOM di un altro componente +- In quali casi è sicuro modificare il DOM gestito da React -## Getting a ref to the node {/*getting-a-ref-to-the-node*/} +## Ottenere un ref al nodo {/*getting-a-ref-to-the-node*/} -To access a DOM node managed by React, first, import the `useRef` Hook: +Per accedere a un nodo DOM gestito da React, per prima cosa importa l'Hook `useRef`: ```js import { useRef } from 'react'; ``` -Then, use it to declare a ref inside your component: +Poi, usalo per dichiarare un ref all'interno del tuo componente: ```js const myRef = useRef(null); ``` -Finally, pass your ref as the `ref` attribute to the JSX tag for which you want to get the DOM node: +Infine, passa il ref come attributo `ref` al tag JSX per cui vuoi ottenere il nodo DOM: ```js
``` -The `useRef` Hook returns an object with a single property called `current`. Initially, `myRef.current` will be `null`. When React creates a DOM node for this `
`, React will put a reference to this node into `myRef.current`. You can then access this DOM node from your [event handlers](/learn/responding-to-events) and use the built-in [browser APIs](https://developer.mozilla.org/docs/Web/API/Element) defined on it. +L'Hook `useRef` restituisce un oggetto con una singola proprietà chiamata `current`. Inizialmente, `myRef.current` sarà `null`. Quando React crea un nodo DOM per questo `
`, React inserirà un riferimento a questo nodo in `myRef.current`. Puoi quindi accedere a questo nodo DOM dai tuoi [gestori di eventi](/learn/responding-to-events) e usare le [API del browser](https://developer.mozilla.org/docs/Web/API/Element) integrate definite su di esso. ```js -// You can use any browser APIs, for example: +// Puoi usare qualsiasi API del browser, ad esempio: myRef.current.scrollIntoView(); ``` -### Example: Focusing a text input {/*example-focusing-a-text-input*/} +### Esempio: mettere a fuoco un input di testo {/*example-focusing-a-text-input*/} -In this example, clicking the button will focus the input: +In questo esempio, cliccare il bottone metterà a fuoco l'input: @@ -73,18 +80,18 @@ export default function Form() { -To implement this: +Per implementarlo: -1. Declare `inputRef` with the `useRef` Hook. -2. Pass it as ``. This tells React to **put this ``'s DOM node into `inputRef.current`.** -3. In the `handleClick` function, read the input DOM node from `inputRef.current` and call [`focus()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) on it with `inputRef.current.focus()`. -4. Pass the `handleClick` event handler to `