Skip to content

Commit fa214de

Browse files
deblasiscursoragent
andcommitted
translate(reference): cloneElement
Signed-off-by: Alessandro De Blasis <alex@deblasis.net> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 876591b commit fa214de

1 file changed

Lines changed: 52 additions & 45 deletions

File tree

src/content/reference/react/cloneElement.md

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
---
22
title: cloneElement
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/cloneElement.md).
9+
10+
</Note>
11+
512
<Pitfall>
613

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)
815

916
</Pitfall>
1017

1118
<Intro>
1219

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.
1421

1522
```js
1623
const clonedElement = cloneElement(element, props, ...children)
@@ -26,7 +33,7 @@ const clonedElement = cloneElement(element, props, ...children)
2633

2734
### `cloneElement(element, props, ...children)` {/*cloneelement*/}
2835

29-
Call `cloneElement` to create a React element based on the `element`, but with different `props` and `children`:
36+
Chiama `cloneElement` per creare un elemento React basato su `element`, ma con `props` e `children` diversi:
3037

3138
```js
3239
import { cloneElement } from 'react';
@@ -43,42 +50,42 @@ const clonedElement = cloneElement(
4350
console.log(clonedElement); // <Row title="Cabbage" isHighlighted={true}>Goodbye</Row>
4451
```
4552

46-
[See more examples below.](#usage)
53+
[Vedi altri esempi sotto.](#usage)
4754

4855
#### Parameters {/*parameters*/}
4956

50-
* `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`.
5158

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.
5360

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.
5562

5663
#### Returns {/*returns*/}
5764

58-
`cloneElement` returns a React element object with a few properties:
65+
`cloneElement` restituisce un oggetto elemento React con alcune proprietà:
5966

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`.
6471

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.
6673

6774
#### Caveats {/*caveats*/}
6875

69-
* Cloning an element **does not modify the original element.**
76+
* Clonare un elemento **non modifica l'elemento originale.**
7077

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.
7279

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).**
7481

7582
---
7683

7784
## Usage {/*usage*/}
7885

79-
### Overriding props of an element {/*overriding-props-of-an-element*/}
86+
### Sovrascrivere le props di un elemento {/*overriding-props-of-an-element*/}
8087

81-
To override the props of some <CodeStep step={1}>React element</CodeStep>, pass it to `cloneElement` with the <CodeStep step={2}>props you want to override</CodeStep>:
88+
Per sovrascrivere le props di un <CodeStep step={1}>elemento React</CodeStep>, passalo a `cloneElement` con le <CodeStep step={2}>props che vuoi sovrascrivere</CodeStep>:
8289

8390
```js [[1, 5, "<Row title=\\"Cabbage\\" />"], [2, 6, "{ isHighlighted: true }"], [3, 4, "clonedElement"]]
8491
import { cloneElement } from 'react';
@@ -90,11 +97,11 @@ const clonedElement = cloneElement(
9097
);
9198
```
9299

93-
Here, the resulting <CodeStep step={3}>cloned element</CodeStep> will be `<Row title="Cabbage" isHighlighted={true} />`.
100+
Qui, l'<CodeStep step={3}>elemento clonato</CodeStep> risultante sarà `<Row title="Cabbage" isHighlighted={true} />`.
94101

95-
**Let's walk through an example to see when it's useful.**
102+
**Vediamo un esempio per capire quando è utile.**
96103

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`:
98105

99106
```js {6-8}
100107
export default function List({ children }) {
@@ -108,7 +115,7 @@ export default function List({ children }) {
108115
)}
109116
```
110117
111-
Let's say the original JSX received by `List` looks like this:
118+
Supponiamo che il JSX originale ricevuto da `List` sia questo:
112119
113120
```js {2-4}
114121
<List>
@@ -118,7 +125,7 @@ Let's say the original JSX received by `List` looks like this:
118125
</List>
119126
```
120127
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:
122129
123130
```js {4,8,12}
124131
<List>
@@ -137,7 +144,7 @@ By cloning its children, the `List` can pass extra information to every `Row` in
137144
</List>
138145
```
139146
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:
141148
142149
<Sandpack>
143150
@@ -232,21 +239,21 @@ button {
232239
233240
</Sandpack>
234241
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.
236243
237244
<Pitfall>
238245
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)
240247
241248
</Pitfall>
242249
243250
---
244251
245252
## Alternatives {/*alternatives*/}
246253
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*/}
248255
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:
250257
251258
```js {1,7}
252259
export default function List({ items, renderItem }) {
@@ -259,7 +266,7 @@ export default function List({ items, renderItem }) {
259266
})}
260267
```
261268
262-
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:
263270
264271
```js {3,7}
265272
<List
@@ -274,7 +281,7 @@ The `renderItem` prop is called a "render prop" because it's a prop that specifi
274281
/>
275282
```
276283
277-
The end result is the same as with `cloneElement`:
284+
Il risultato finale è lo stesso di `cloneElement`:
278285
279286
```js {4,8,12}
280287
<List>
@@ -293,7 +300,7 @@ The end result is the same as with `cloneElement`:
293300
</List>
294301
```
295302
296-
However, you can clearly trace where the `isHighlighted` value is coming from.
303+
Tuttavia, puoi tracciare chiaramente da dove proviene il valore `isHighlighted`.
297304
298305
<Sandpack>
299306
@@ -389,22 +396,22 @@ button {
389396
390397
</Sandpack>
391398
392-
This pattern is preferred to `cloneElement` because it is more explicit.
399+
Questo pattern è preferibile a `cloneElement` perché è più esplicito.
393400
394401
---
395402
396-
### Passing data through context {/*passing-data-through-context*/}
403+
### Passare dati tramite context {/*passing-data-through-context*/}
397404
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)
399406
400407
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`:
402409
403410
```js
404411
export const HighlightContext = createContext(false);
405412
```
406413
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`:
408415
409416
```js {8,10}
410417
export default function List({ items, renderItem }) {
@@ -421,15 +428,15 @@ export default function List({ items, renderItem }) {
421428
})}
422429
```
423430
424-
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:
425432
426433
```js src/Row.js {2}
427434
export default function Row({ title }) {
428435
const isHighlighted = useContext(HighlightContext);
429436
// ...
430437
```
431438
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>`:
433440
434441
```js {4}
435442
<List
@@ -440,7 +447,7 @@ This allows the calling component to not know or worry about passing `isHighligh
440447
/>
441448
```
442449
443-
Instead, `List` and `Row` coordinate the highlighting logic through context.
450+
Invece, `List` e `Row` coordinano la logica di evidenziazione tramite context.
444451
445452
<Sandpack>
446453
@@ -550,13 +557,13 @@ button {
550557
551558
</Sandpack>
552559
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)
554561
555562
---
556563
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*/}
558565
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:
560567
561568
```js
562569
import { useState } from 'react';
@@ -575,7 +582,7 @@ export default function useList(items) {
575582
}
576583
```
577584
578-
Then you could use it like this:
585+
Poi puoi usarlo così:
579586
580587
```js {2,9,13}
581588
export default function App() {
@@ -598,7 +605,7 @@ export default function App() {
598605
}
599606
```
600607
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:
602609
603610
<Sandpack>
604611
@@ -691,4 +698,4 @@ button {
691698
692699
</Sandpack>
693700
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

Comments
 (0)