Skip to content

Commit ca60533

Browse files
authored
translate(reference): createPortal (#669) (#702)
Signed-off-by: Alessandro De Blasis <alex@deblasis.net>
1 parent d81e665 commit ca60533

1 file changed

Lines changed: 55 additions & 48 deletions

File tree

src/content/reference/react-dom/createPortal.md

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

7-
`createPortal` lets you render some children into a different part of the DOM.
14+
`createPortal` ti permette di renderizzare alcuni figli in una parte diversa del DOM.
815

916

1017
```js
@@ -24,71 +31,71 @@ title: createPortal
2431
2532
### `createPortal(children, domNode, key?)` {/*createportal*/}
2633
27-
To create a portal, call `createPortal`, passing some JSX, and the DOM node where it should be rendered:
34+
Per creare un portal, chiama `createPortal` passando del JSX e il nodo DOM in cui deve essere renderizzato:
2835
2936
```js
3037
import { createPortal } from 'react-dom';
3138

3239
// ...
3340

3441
<div>
35-
<p>This child is placed in the parent div.</p>
42+
<p>Questo figlio è posizionato nel div genitore.</p>
3643
{createPortal(
37-
<p>This child is placed in the document body.</p>,
44+
<p>Questo figlio è posizionato nel body del documento.</p>,
3845
document.body
3946
)}
4047
</div>
4148
```
4249
43-
[See more examples below.](#usage)
50+
[Vedi altri esempi sotto.](#usage)
4451
45-
A portal only changes the physical placement of the DOM node. In every other way, the JSX you render into a portal acts as a child node of the React component that renders it. For example, the child can access the context provided by the parent tree, and events bubble up from children to parents according to the React tree.
52+
Un portal modifica solo il posizionamento fisico del nodo DOM. Per il resto, il JSX che renderizzi in un portal si comporta come un nodo figlio del componente React che lo renderizza. Ad esempio, il figlio può accedere al context fornito dall'albero genitore e gli eventi risalgono dai figli ai genitori secondo l'albero React.
4653
4754
#### Parameters {/*parameters*/}
4855
49-
* `children`: Anything that can be rendered with React, such as a piece of JSX (e.g. `<div />` or `<SomeComponent />`), a [Fragment](/reference/react/Fragment) (`<>...</>`), a string or a number, or an array of these.
56+
* `children`: Qualsiasi cosa che possa essere renderizzata con React, come un pezzo di JSX (ad es. `<div />` o `<SomeComponent />`), un [Fragment](/reference/react/Fragment) (`<>...</>`), una stringa o un numero, oppure un array di questi.
5057
51-
* `domNode`: Some DOM node, such as those returned by `document.getElementById()`. The node must already exist. Passing a different DOM node during an update will cause the portal content to be recreated.
58+
* `domNode`: Un nodo DOM, come quelli restituiti da `document.getElementById()`. Il nodo deve già esistere. Passare un nodo DOM diverso durante un aggiornamento farà ricreare il contenuto del portal.
5259
53-
* **optional** `key`: A unique string or number to be used as the portal's [key.](/learn/rendering-lists#keeping-list-items-in-order-with-key)
60+
* **optional** `key`: Una stringa o un numero univoco da usare come [key](/learn/rendering-lists#keeping-list-items-in-order-with-key) del portal.
5461
5562
#### Returns {/*returns*/}
5663
57-
`createPortal` returns a React node that can be included into JSX or returned from a React component. If React encounters it in the render output, it will place the provided `children` inside the provided `domNode`.
64+
`createPortal` restituisce un nodo React che può essere incluso nel JSX o restituito da un componente React. Se React lo incontra nell'output di renderizzazione, posizionerà i `children` forniti all'interno del `domNode` fornito.
5865
5966
#### Caveats {/*caveats*/}
6067
61-
* Events from portals propagate according to the React tree rather than the DOM tree. For example, if you click inside a portal, and the portal is wrapped in `<div onClick>`, that `onClick` handler will fire. If this causes issues, either stop the event propagation from inside the portal, or move the portal itself up in the React tree.
68+
* Gli eventi dai portal si propagano secondo l'albero React anziché l'albero DOM. Ad esempio, se fai clic all'interno di un portal e il portal è avvolto in `<div onClick>`, verrà eseguito quel gestore di eventi `onClick`. Se questo causa problemi, interrompi la propagazione dell'evento dall'interno del portal oppure sposta il portal stesso più in alto nell'albero React.
6269
6370
---
6471
6572
## Usage {/*usage*/}
6673
67-
### Rendering to a different part of the DOM {/*rendering-to-a-different-part-of-the-dom*/}
74+
### Renderizzare in una parte diversa del DOM {/*rendering-to-a-different-part-of-the-dom*/}
6875
69-
*Portals* let your components render some of their children into a different place in the DOM. This lets a part of your component "escape" from whatever containers it may be in. For example, a component can display a modal dialog or a tooltip that appears above and outside of the rest of the page.
76+
I *portal* permettono ai tuoi componenti di renderizzare alcuni dei loro figli in un punto diverso del DOM. Così una parte del componente può "uscire" da qualunque contenitore in cui si trovi. Ad esempio, un componente può mostrare una finestra modale o un tooltip che appare sopra e fuori dal resto della pagina.
7077
71-
To create a portal, render the result of `createPortal` with <CodeStep step={1}>some JSX</CodeStep> and the <CodeStep step={2}>DOM node where it should go</CodeStep>:
78+
Per creare un portal, renderizza il risultato di `createPortal` con <CodeStep step={1}>del JSX</CodeStep> e il <CodeStep step={2}>nodo DOM in cui deve andare</CodeStep>:
7279
73-
```js [[1, 8, "<p>This child is placed in the document body.</p>"], [2, 9, "document.body"]]
80+
```js [[1, 8, "<p>Questo figlio è posizionato nel body del documento.</p>"], [2, 9, "document.body"]]
7481
import { createPortal } from 'react-dom';
7582

7683
function MyComponent() {
7784
return (
7885
<div style={{ border: '2px solid black' }}>
79-
<p>This child is placed in the parent div.</p>
86+
<p>Questo figlio è posizionato nel div genitore.</p>
8087
{createPortal(
81-
<p>This child is placed in the document body.</p>,
88+
<p>Questo figlio è posizionato nel body del documento.</p>,
8289
document.body
8390
)}
8491
</div>
8592
);
8693
}
8794
```
8895
89-
React will put the DOM nodes for <CodeStep step={1}>the JSX you passed</CodeStep> inside of the <CodeStep step={2}>DOM node you provided</CodeStep>.
96+
React inserirà i nodi DOM del <CodeStep step={1}>JSX che hai passato</CodeStep> all'interno del <CodeStep step={2}>nodo DOM che hai fornito</CodeStep>.
9097
91-
Without a portal, the second `<p>` would be placed inside the parent `<div>`, but the portal "teleported" it into the [`document.body`:](https://developer.mozilla.org/en-US/docs/Web/API/Document/body)
98+
Senza un portal, il secondo `<p>` sarebbe posizionato all'interno del `<div>` genitore, ma il portal lo ha "teletrasportato" nel [`document.body`:](https://developer.mozilla.org/it/docs/Web/API/Document/body)
9299
93100
<Sandpack>
94101
@@ -98,9 +105,9 @@ import { createPortal } from 'react-dom';
98105
export default function MyComponent() {
99106
return (
100107
<div style={{ border: '2px solid black' }}>
101-
<p>This child is placed in the parent div.</p>
108+
<p>Questo figlio è posizionato nel div genitore.</p>
102109
{createPortal(
103-
<p>This child is placed in the document body.</p>,
110+
<p>Questo figlio è posizionato nel body del documento.</p>,
104111
document.body
105112
)}
106113
</div>
@@ -110,30 +117,30 @@ export default function MyComponent() {
110117
111118
</Sandpack>
112119
113-
Notice how the second paragraph visually appears outside the parent `<div>` with the border. If you inspect the DOM structure with developer tools, you'll see that the second `<p>` got placed directly into the `<body>`:
120+
Nota come il secondo paragrafo appare visivamente fuori dal `<div>` genitore con il bordo. Se ispezioni la struttura DOM con gli strumenti per sviluppatori, vedrai che il secondo `<p>` è stato posizionato direttamente nel `<body>`:
114121
115122
```html {4-6,9}
116123
<body>
117124
<div id="root">
118125
...
119126
<div style="border: 2px solid black">
120-
<p>This child is placed inside the parent div.</p>
127+
<p>Questo figlio è posizionato nel div genitore.</p>
121128
</div>
122129
...
123130
</div>
124-
<p>This child is placed in the document body.</p>
131+
<p>Questo figlio è posizionato nel body del documento.</p>
125132
</body>
126133
```
127134
128-
A portal only changes the physical placement of the DOM node. In every other way, the JSX you render into a portal acts as a child node of the React component that renders it. For example, the child can access the context provided by the parent tree, and events still bubble up from children to parents according to the React tree.
135+
Un portal modifica solo il posizionamento fisico del nodo DOM. Per il resto, il JSX che renderizzi in un portal si comporta come un nodo figlio del componente React che lo renderizza. Ad esempio, il figlio può accedere al context fornito dall'albero genitore e gli eventi risalgono comunque dai figli ai genitori secondo l'albero React.
129136
130137
---
131138
132-
### Rendering a modal dialog with a portal {/*rendering-a-modal-dialog-with-a-portal*/}
139+
### Renderizzare una finestra modale con un portal {/*rendering-a-modal-dialog-with-a-portal*/}
133140
134-
You can use a portal to create a modal dialog that floats above the rest of the page, even if the component that summons the dialog is inside a container with `overflow: hidden` or other styles that interfere with the dialog.
141+
Puoi usare un portal per creare una finestra modale che fluttua sopra il resto della pagina, anche se il componente che la invoca si trova dentro un contenitore con `overflow: hidden` o altri stili che interferiscono con la finestra.
135142
136-
In this example, the two containers have styles that disrupt the modal dialog, but the one rendered into a portal is unaffected because, in the DOM, the modal is not contained within the parent JSX elements.
143+
In questo esempio, i due contenitori hanno stili che disturbano la finestra modale, ma quella renderizzata in un portal non ne è influenzata perché, nel DOM, la modale non è contenuta negli elementi JSX genitori.
137144
138145
<Sandpack>
139146
@@ -164,7 +171,7 @@ export default function NoPortalExample() {
164171
return (
165172
<>
166173
<button onClick={() => setShowModal(true)}>
167-
Show modal without a portal
174+
Mostra modale senza portal
168175
</button>
169176
{showModal && (
170177
<ModalContent onClose={() => setShowModal(false)} />
@@ -184,7 +191,7 @@ export default function PortalExample() {
184191
return (
185192
<>
186193
<button onClick={() => setShowModal(true)}>
187-
Show modal using a portal
194+
Mostra modale con un portal
188195
</button>
189196
{showModal && createPortal(
190197
<ModalContent onClose={() => setShowModal(false)} />,
@@ -199,8 +206,8 @@ export default function PortalExample() {
199206
export default function ModalContent({ onClose }) {
200207
return (
201208
<div className="modal">
202-
<div>I'm a modal dialog</div>
203-
<button onClick={onClose}>Close</button>
209+
<div>Sono una finestra modale</div>
210+
<button onClick={onClose}>Chiudi</button>
204211
</div>
205212
);
206213
}
@@ -238,29 +245,29 @@ export default function ModalContent({ onClose }) {
238245
239246
<Pitfall>
240247
241-
It's important to make sure that your app is accessible when using portals. For instance, you may need to manage keyboard focus so that the user can move the focus in and out of the portal in a natural way.
248+
È importante assicurarsi che l'app sia accessibile quando usi i portal. Ad esempio, potresti dover gestire il focus da tastiera in modo che l'utente possa spostarlo dentro e fuori dal portal in modo naturale.
242249
243-
Follow the [WAI-ARIA Modal Authoring Practices](https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal) when creating modals. If you use a community package, ensure that it is accessible and follows these guidelines.
250+
Segui le [WAI-ARIA Modal Authoring Practices](https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal) quando crei modali. Se usi un pacchetto della community, assicurati che sia accessibile e segua queste linee guida.
244251
245252
</Pitfall>
246253
247254
---
248255
249-
### Rendering React components into non-React server markup {/*rendering-react-components-into-non-react-server-markup*/}
256+
### Renderizzare componenti React in markup server non React {/*rendering-react-components-into-non-react-server-markup*/}
250257
251-
Portals can be useful if your React root is only part of a static or server-rendered page that isn't built with React. For example, if your page is built with a server framework like Rails, you can create areas of interactivity within static areas such as sidebars. Compared with having [multiple separate React roots,](/reference/react-dom/client/createRoot#rendering-a-page-partially-built-with-react) portals let you treat the app as a single React tree with shared state even though its parts render to different parts of the DOM.
258+
I portal possono essere utili se la root React è solo una parte di una pagina statica o renderizzata lato server che non è costruita con React. Ad esempio, se la pagina è costruita con un framework server come Rails, puoi creare aree di interattività all'interno di aree statiche come le sidebar. Rispetto ad avere [più root React separate,](/reference/react-dom/client/createRoot#rendering-a-page-partially-built-with-react) i portal ti permettono di trattare l'app come un unico albero React con state condiviso anche se le sue parti renderizzano in punti diversi del DOM.
252259
253260
<Sandpack>
254261
255262
```html public/index.html
256263
<!DOCTYPE html>
257264
<html>
258-
<head><title>My app</title></head>
265+
<head><title>La mia app</title></head>
259266
<body>
260-
<h1>Welcome to my hybrid app</h1>
267+
<h1>Benvenuto nella mia app ibrida</h1>
261268
<div class="parent">
262269
<div class="sidebar">
263-
This is server non-React markup
270+
Questo è markup server non React
264271
<div id="sidebar-content"></div>
265272
</div>
266273
<div id="root"></div>
@@ -301,11 +308,11 @@ export default function App() {
301308
}
302309

303310
function MainContent() {
304-
return <p>This part is rendered by React</p>;
311+
return <p>Questa parte è renderizzata da React</p>;
305312
}
306313

307314
function SidebarContent() {
308-
return <p>This part is also rendered by React!</p>;
315+
return <p>Anche questa parte è renderizzata da React!</p>;
309316
}
310317
```
311318
@@ -342,15 +349,15 @@ p {
342349
343350
---
344351
345-
### Rendering React components into non-React DOM nodes {/*rendering-react-components-into-non-react-dom-nodes*/}
352+
### Renderizzare componenti React in nodi DOM non React {/*rendering-react-components-into-non-react-dom-nodes*/}
346353
347-
You can also use a portal to manage the content of a DOM node that's managed outside of React. For example, suppose you're integrating with a non-React map widget and you want to render React content inside a popup. To do this, declare a `popupContainer` state variable to store the DOM node you're going to render into:
354+
Puoi anche usare un portal per gestire il contenuto di un nodo DOM gestito al di fuori di React. Ad esempio, supponiamo che tu stia integrando un widget mappa non React e voglia renderizzare contenuto React all'interno di un popup. Per farlo, dichiara una variabile di state `popupContainer` per memorizzare il nodo DOM in cui renderizzerai:
348355
349356
```js
350357
const [popupContainer, setPopupContainer] = useState(null);
351358
```
352359
353-
When you create the third-party widget, store the DOM node returned by the widget so you can render into it:
360+
Quando crei il widget di terze parti, memorizza il nodo DOM restituito dal widget così puoi renderizzare al suo interno:
354361
355362
```js {5-6}
356363
useEffect(() => {
@@ -363,20 +370,20 @@ useEffect(() => {
363370
}, []);
364371
```
365372
366-
This lets you use `createPortal` to render React content into `popupContainer` once it becomes available:
373+
Questo ti permette di usare `createPortal` per renderizzare contenuto React in `popupContainer` non appena diventa disponibile:
367374
368375
```js {3-6}
369376
return (
370377
<div style={{ width: 250, height: 250 }} ref={containerRef}>
371378
{popupContainer !== null && createPortal(
372-
<p>Hello from React!</p>,
379+
<p>Ciao da React!</p>,
373380
popupContainer
374381
)}
375382
</div>
376383
);
377384
```
378385
379-
Here is a complete example you can play with:
386+
Ecco un esempio completo con cui puoi sperimentare:
380387
381388
<Sandpack>
382389
@@ -420,7 +427,7 @@ export default function Map() {
420427
return (
421428
<div style={{ width: 250, height: 250 }} ref={containerRef}>
422429
{popupContainer !== null && createPortal(
423-
<p>Hello from React!</p>,
430+
<p>Ciao da React!</p>,
424431
popupContainer
425432
)}
426433
</div>

0 commit comments

Comments
 (0)