You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/reference/react/cloneElement.md
+52-45Lines changed: 52 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,23 @@
1
1
---
2
2
title: cloneElement
3
+
translationStatus: ai-draft
3
4
---
4
5
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/cloneElement.md).
9
+
10
+
</Note>
11
+
5
12
<Pitfall>
6
13
7
-
Using `cloneElement`is uncommon and can lead to fragile code. [See common alternatives.](#alternatives)
14
+
L'uso di `cloneElement`è poco comune e può portare a codice fragile. [Vedi le alternative comuni.](#alternatives)
8
15
9
16
</Pitfall>
10
17
11
18
<Intro>
12
19
13
-
`cloneElement`lets you create a new React element using another element as a starting point.
20
+
`cloneElement`ti permette di creare un nuovo elemento React usando un altro elemento come punto di partenza.
*`element`: The`element`argument must be a valid React element. For example, it could be a JSX node like `<Something />`, the result of calling [`createElement`](/reference/react/createElement), or the result of another `cloneElement` call.
57
+
*`element`: L'argomento`element`deve essere un elemento React valido. Ad esempio, può essere un nodo JSX come `<Something />`, il risultato di una chiamata a [`createElement`](/reference/react/createElement) o il risultato di un'altra chiamata a `cloneElement`.
51
58
52
-
*`props`: The`props`argument must either be an object or `null`. If you pass `null`, the cloned element will retain all of the original `element.props`. Otherwise, for every prop in the `props` object, the returned element will "prefer" the value from`props`over the value from`element.props`. The rest of the props will be filled from the original `element.props`. If you pass `props.key`or`props.ref`, they will replace the original ones.
59
+
*`props`: L'argomento`props`deve essere un oggetto oppure `null`. Se passi `null`, l'elemento clonato conserverà tutte le `element.props` originali. Altrimenti, per ogni prop nell'oggetto `props`, l'elemento restituito "preferirà" il valore da`props`rispetto al valore da`element.props`. Le altre props verranno prese dalle `element.props` originali. Se passi `props.key`o`props.ref`, sostituiranno quelli originali.
53
60
54
-
***optional**`...children`: Zero or more child nodes. They can be any React nodes, including React elements, strings, numbers, [portals](/reference/react-dom/createPortal), empty nodes (`null`, `undefined`, `true`, and`false`), and arrays of React nodes. If you don't pass any `...children` arguments, the original `element.props.children`will be preserved.
61
+
***optional**`...children`: Zero o più nodi figli. Possono essere qualsiasi nodo React, inclusi elementi React, stringhe, numeri, [portali](/reference/react-dom/createPortal), nodi vuoti (`null`, `undefined`, `true` e`false`) e array di nodi React. Se non passi argomenti `...children`, verranno preservati gli `element.props.children`originali.
55
62
56
63
#### Returns {/*returns*/}
57
64
58
-
`cloneElement`returns a React element object with a few properties:
65
+
`cloneElement`restituisce un oggetto elemento React con alcune proprietà:
59
66
60
-
*`type`: Same as`element.type`.
61
-
*`props`: The result of shallowly merging `element.props`with the overriding `props`you have passed.
62
-
*`ref`: The original `element.ref`, unless it was overridden by`props.ref`.
63
-
*`key`: The original `element.key`, unless it was overridden by`props.key`.
67
+
*`type`: Uguale a`element.type`.
68
+
*`props`: Il risultato della fusione superficiale di `element.props`con le `props`di override che hai passato.
69
+
*`ref`: La `element.ref` originale, a meno che non sia stata sovrascritta da`props.ref`.
70
+
*`key`: La `element.key` originale, a meno che non sia stata sovrascritta da`props.key`.
64
71
65
-
Usually, you'll return the element from your component or make it a child of another element. Although you may read the element's properties, it's best to treat every element as opaque after it's created, and only render it.
72
+
Di solito restituirai l'elemento dal tuo componente o lo renderai come figlio di un altro elemento. Anche se puoi leggere le proprietà dell'elemento, è meglio trattare ogni elemento come opaco dopo la creazione e limitarti a renderizzarlo.
66
73
67
74
#### Caveats {/*caveats*/}
68
75
69
-
*Cloning an element**does not modify the original element.**
76
+
*Clonare un elemento**non modifica l'elemento originale.**
70
77
71
-
*You should only **pass children as multiple arguments to`cloneElement`if they are all statically known,**like`cloneElement(element, null, child1, child2, child3)`. If your children are dynamic, pass the entire array as the third argument: `cloneElement(element, null, listItems)`. This ensures that React will [warn you about missing `key`s](/learn/rendering-lists#keeping-list-items-in-order-with-key)for any dynamic lists. For static lists this is not necessary because they never reorder.
78
+
*Dovresti **passare i children come argomenti multipli a`cloneElement`solo se sono tutti staticamente noti,**come`cloneElement(element, null, child1, child2, child3)`. Se i tuoi children sono dinamici, passa l'intero array come terzo argomento: `cloneElement(element, null, listItems)`. In questo modo React ti [avviserà delle `key` mancanti](/learn/rendering-lists#keeping-list-items-in-order-with-key)per qualsiasi lista dinamica. Per le liste statiche non è necessario, perché non vengono mai riordinate.
72
79
73
-
*`cloneElement`makes it harder to trace the data flow, so**try the [alternatives](#alternatives) instead.**
80
+
*`cloneElement`rende più difficile tracciare il flusso dei dati, quindi**prova le [alternative](#alternatives).**
74
81
75
82
---
76
83
77
84
## Usage {/*usage*/}
78
85
79
-
### Overriding props of an element {/*overriding-props-of-an-element*/}
86
+
### Sovrascrivere le props di un elemento {/*overriding-props-of-an-element*/}
80
87
81
-
To override the props of some <CodeStepstep={1}>React element</CodeStep>, pass it to `cloneElement`with the <CodeStepstep={2}>props you want to override</CodeStep>:
88
+
Per sovrascrivere le props di un <CodeStepstep={1}>elemento React</CodeStep>, passalo a `cloneElement`con le <CodeStepstep={2}>props che vuoi sovrascrivere</CodeStep>:
Here, the resulting <CodeStepstep={3}>cloned element</CodeStep> will be`<Row title="Cabbage" isHighlighted={true} />`.
100
+
Qui, l'<CodeStepstep={3}>elemento clonato</CodeStep> risultante sarà`<Row title="Cabbage" isHighlighted={true} />`.
94
101
95
-
**Let's walk through an example to see when it's useful.**
102
+
**Vediamo un esempio per capire quando è utile.**
96
103
97
-
Imagine a `List`component that renders its[`children`](/learn/passing-props-to-a-component#passing-jsx-as-children)as a list of selectable rows with a "Next" button that changes which row is selected. The `List`component needs to render the selected`Row`differently, so it clones every`<Row>`child that it has received, and adds an extra `isHighlighted: true`or`isHighlighted: false` prop:
104
+
Immagina un componente `List`che renderizza i suoi[`children`](/learn/passing-props-to-a-component#passing-jsx-as-children)come lista di righe selezionabili con un pulsante "Next" che cambia quale riga è selezionata. Il componente `List`deve renderizzare in modo diverso la`Row`selezionata, quindi clona ogni child`<Row>`che ha ricevuto e aggiunge una prop extra `isHighlighted: true`o`isHighlighted: false`:
98
105
99
106
```js {6-8}
100
107
exportdefaultfunctionList({ children }) {
@@ -108,7 +115,7 @@ export default function List({ children }) {
108
115
)}
109
116
```
110
117
111
-
Let's say the original JSX received by `List`looks like this:
118
+
Supponiamo che il JSX originale ricevuto da `List`sia questo:
112
119
113
120
```js {2-4}
114
121
<List>
@@ -118,7 +125,7 @@ Let's say the original JSX received by `List` looks like this:
118
125
</List>
119
126
```
120
127
121
-
By cloning its children, the `List`can pass extra information to every `Row`inside. The result looks like this:
128
+
Clonando i suoi children, `List`può passare informazioni extra a ogni `Row`al suo interno. Il risultato è simile a questo:
122
129
123
130
```js {4,8,12}
124
131
<List>
@@ -137,7 +144,7 @@ By cloning its children, the `List` can pass extra information to every `Row` in
137
144
</List>
138
145
```
139
146
140
-
Notice how pressing "Next" updates the state of the `List`, and highlights a different row:
147
+
Nota come premere "Next" aggiorna lo state di `List` ed evidenzia una riga diversa:
141
148
142
149
<Sandpack>
143
150
@@ -232,21 +239,21 @@ button {
232
239
233
240
</Sandpack>
234
241
235
-
To summarize, the `List`cloned the `<Row />`elements it received and added an extra prop to them.
242
+
In sintesi, `List`ha clonato gli elementi `<Row />`che ha ricevuto e ha aggiunto loro una prop extra.
236
243
237
244
<Pitfall>
238
245
239
-
Cloning children makes it hard to tell how the data flows through your app. Try one of the [alternatives.](#alternatives)
246
+
Clonare i children rende difficile capire come i dati scorrono nella tua app. Prova una delle [alternative.](#alternatives)
240
247
241
248
</Pitfall>
242
249
243
250
---
244
251
245
252
## Alternatives {/*alternatives*/}
246
253
247
-
### Passing data with a render prop {/*passing-data-with-a-render-prop*/}
254
+
### Passare dati con una render prop {/*passing-data-with-a-render-prop*/}
248
255
249
-
Instead of using`cloneElement`, consider accepting a *render prop* like`renderItem`. Here, `List`receives`renderItem`as a prop. `List`calls`renderItem`for every item and passes`isHighlighted`as an argument:
256
+
Invece di usare`cloneElement`, valuta di accettare una *render prop* come`renderItem`. Qui, `List`riceve`renderItem`come prop. `List`chiama`renderItem`per ogni elemento e passa`isHighlighted`come argomento:
The `renderItem`prop is called a "render prop" because it's a prop that specifies how to render something. For example, you can pass a`renderItem`implementation that renders a `<Row>`with the given`isHighlighted`value:
269
+
La prop `renderItem`si chiama "render prop" perché è una prop che specifica come renderizzare qualcosa. Ad esempio, puoi passare un'implementazione di`renderItem`che renderizza una `<Row>`con il valore`isHighlighted`dato:
263
270
264
271
```js {3,7}
265
272
<List
@@ -274,7 +281,7 @@ The `renderItem` prop is called a "render prop" because it's a prop that specifi
274
281
/>
275
282
```
276
283
277
-
The end result is the same as with`cloneElement`:
284
+
Il risultato finale è lo stesso di`cloneElement`:
278
285
279
286
```js {4,8,12}
280
287
<List>
@@ -293,7 +300,7 @@ The end result is the same as with `cloneElement`:
293
300
</List>
294
301
```
295
302
296
-
However, you can clearly trace where the `isHighlighted` value is coming from.
303
+
Tuttavia, puoi tracciare chiaramente da dove proviene il valore `isHighlighted`.
297
304
298
305
<Sandpack>
299
306
@@ -389,22 +396,22 @@ button {
389
396
390
397
</Sandpack>
391
398
392
-
This pattern is preferred to`cloneElement`because it is more explicit.
399
+
Questo pattern è preferibile a`cloneElement`perché è più esplicito.
393
400
394
401
---
395
402
396
-
### Passing data through context {/*passing-data-through-context*/}
403
+
### Passare dati tramite context {/*passing-data-through-context*/}
397
404
398
-
Another alternative to`cloneElement`is to [pass data through context.](/learn/passing-data-deeply-with-context)
405
+
Un'altra alternativa a`cloneElement`è [passare dati tramite context.](/learn/passing-data-deeply-with-context)
399
406
400
407
401
-
For example, you can call [`createContext`](/reference/react/createContext) to define a`HighlightContext`:
408
+
Ad esempio, puoi chiamare [`createContext`](/reference/react/createContext) per definire un`HighlightContext`:
402
409
403
410
```js
404
411
exportconstHighlightContext=createContext(false);
405
412
```
406
413
407
-
Your `List`component can wrap every item it renders into a`HighlightContext` provider:
414
+
Il tuo componente `List`può avvolgere ogni elemento che renderizza in un provider`HighlightContext`:
With this approach, `Row`does not need to receive an`isHighlighted` prop at all. Instead, it reads the context:
431
+
Con questo approccio, `Row`non deve ricevere affatto una prop`isHighlighted`. Legge invece il context:
425
432
426
433
```js src/Row.js {2}
427
434
exportdefaultfunctionRow({ title }) {
428
435
constisHighlighted=useContext(HighlightContext);
429
436
// ...
430
437
```
431
438
432
-
This allows the calling component to not know or worry about passing `isHighlighted`to`<Row>`:
439
+
In questo modo il componente chiamante non deve conoscere o preoccuparsi di passare `isHighlighted`a`<Row>`:
433
440
434
441
```js {4}
435
442
<List
@@ -440,7 +447,7 @@ This allows the calling component to not know or worry about passing `isHighligh
440
447
/>
441
448
```
442
449
443
-
Instead, `List`and`Row`coordinate the highlighting logic through context.
450
+
Invece, `List`e`Row`coordinano la logica di evidenziazione tramite context.
444
451
445
452
<Sandpack>
446
453
@@ -550,13 +557,13 @@ button {
550
557
551
558
</Sandpack>
552
559
553
-
[Learn more about passing data through context.](/reference/react/useContext#passing-data-deeply-into-the-tree)
560
+
[Scopri di più sul passaggio di dati tramite context.](/reference/react/useContext#passing-data-deeply-into-the-tree)
554
561
555
562
---
556
563
557
-
### Extracting logic into a custom Hook {/*extracting-logic-into-a-custom-hook*/}
564
+
### Estrarre la logica in un custom hook {/*extracting-logic-into-a-custom-hook*/}
558
565
559
-
Another approach you can try is to extract the "non-visual" logic into your own Hook, and use the information returned by your Hook to decide what to render. For example, you could write a `useList`custom Hook like this:
566
+
Un altro approccio che puoi provare è estrarre la logica "non visuale" in un tuo Hook e usare le informazioni restituite dall'Hook per decidere cosa renderizzare. Ad esempio, puoi scrivere un custom hook `useList`come questo:
560
567
561
568
```js
562
569
import { useState } from'react';
@@ -575,7 +582,7 @@ export default function useList(items) {
575
582
}
576
583
```
577
584
578
-
Then you could use it like this:
585
+
Poi puoi usarlo così:
579
586
580
587
```js {2,9,13}
581
588
exportdefaultfunctionApp() {
@@ -598,7 +605,7 @@ export default function App() {
598
605
}
599
606
```
600
607
601
-
The data flow is explicit, but the state is inside the `useList`custom Hook that you can use from any component:
608
+
Il flusso dei dati è esplicito, ma lo state è dentro il custom hook `useList`che puoi usare da qualsiasi componente:
602
609
603
610
<Sandpack>
604
611
@@ -691,4 +698,4 @@ button {
691
698
692
699
</Sandpack>
693
700
694
-
This approach is particularly useful if you want to reuse this logic between different components.
701
+
Questo approccio è particolarmente utile se vuoi riutilizzare questa logica tra componenti diversi.
0 commit comments