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/createElement.md
+37-30Lines changed: 37 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,17 @@
1
1
---
2
2
title: createElement
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/createElement.md).
9
+
10
+
</Note>
11
+
5
12
<Intro>
6
13
7
-
`createElement`lets you create a React element. It serves as an alternative to writing [JSX.](/learn/writing-markup-with-jsx)
14
+
`createElement`ti permette di creare un elemento React. Funziona come alternativa alla scrittura del [JSX](/learn/writing-markup-with-jsx).
Call`createElement`to create a React element with the given `type`, `props`, and `children`.
30
+
Chiama`createElement`per creare un elemento React con il `type`, le `props` e i `children` indicati.
24
31
25
32
```js
26
33
import { createElement } from'react';
@@ -34,44 +41,44 @@ function Greeting({ name }) {
34
41
}
35
42
```
36
43
37
-
[See more examples below.](#usage)
44
+
[Vedi altri esempi sotto.](#usage)
38
45
39
46
#### Parameters {/*parameters*/}
40
47
41
-
*`type`: The`type`argument must be a valid React component type. For example, it could be a tag name string (such as `'div'`or`'span'`), or a React component (a function, a class, or a special component like[`Fragment`](/reference/react/Fragment)).
48
+
*`type`: L'argomento`type`deve essere un tipo componente React valido. Ad esempio, può essere una stringa con il nome di un tag (come `'div'`o`'span'`), oppure un componente React (una funzione, una classe o un componente speciale come[`Fragment`](/reference/react/Fragment)).
42
49
43
-
*`props`: The`props`argument must either be an object or `null`. If you pass `null`, it will be treated the same as an empty object. React will create an element with props matching the `props`you have passed. Note that`ref`and`key`from your `props`object are special and will *not* be available as`element.props.ref`and`element.props.key`on the returned `element`. They will be available as `element.ref`and`element.key`.
50
+
*`props`: L'argomento`props`deve essere un oggetto oppure `null`. Se passi `null`, verrà trattato come un oggetto vuoto. React creerà un elemento con props che corrispondono alle `props`che hai passato. Nota che`ref`e`key`dall'oggetto `props`sono speciali e *non* saranno disponibili come`element.props.ref`e`element.props.key`sull' `element` restituito. Saranno disponibili come `element.ref`e`element.key`.
44
51
45
-
***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.
52
+
***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.
46
53
47
54
#### Returns {/*returns*/}
48
55
49
-
`createElement`returns a React element object with a few properties:
56
+
`createElement`restituisce un oggetto elemento React con alcune proprietà:
50
57
51
-
*`type`: The`type`you have passed.
52
-
*`props`: The`props`you have passed except for `ref`and`key`.
53
-
*`ref`: The`ref`you have passed. If missing, `null`.
54
-
*`key`: The`key`you have passed, coerced to a string. If missing, `null`.
58
+
*`type`: Il`type`che hai passato.
59
+
*`props`: Le`props`che hai passato, tranne `ref`e`key`.
60
+
*`ref`: La`ref`che hai passato. Se manca, `null`.
61
+
*`key`: La`key`che hai passato, convertita in stringa. Se manca, `null`.
55
62
56
-
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.
63
+
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.
57
64
58
65
#### Caveats {/*caveats*/}
59
66
60
-
*You must **treat React elements and their props as [immutable](https://en.wikipedia.org/wiki/Immutable_object)**and never change their contents after creation. In development, React will [freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze)the returned element and its `props`property shallowly to enforce this.
67
+
*Devi **trattare gli elementi React e le loro props come [immutabili](https://it.wikipedia.org/wiki/Struttura_dati_persistente)**e non modificarne mai il contenuto dopo la creazione. In sviluppo, React [congelerà](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze)l'elemento restituito e la sua proprietà `props`in modo superficiale per imporre questa regola.
61
68
62
-
*When you use JSX, **you must start a tag with a capital letter to render your own custom component.** In other words, `<Something />`is equivalent to `createElement(Something)`, but`<something />` (lowercase) is equivalent to `createElement('something')` (note it's a string, so it will be treated as a built-in HTML tag).
69
+
*Quando usi JSX, **devi iniziare un tag con una lettera maiuscola per renderizzare il tuo componente personalizzato.** In altre parole, `<Something />`equivale a `createElement(Something)`, ma`<something />` (minuscolo) equivale a `createElement('something')` (nota che è una stringa, quindi verrà trattato come un tag HTML integrato).
63
70
64
-
*You should only **pass children as multiple arguments to`createElement`if they are all statically known,**like`createElement('h1', {}, child1, child2, child3)`. If your children are dynamic, pass the entire array as the third argument: `createElement('ul', {}, 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.
71
+
*Dovresti **passare i children come argomenti multipli a`createElement`solo se sono tutti staticamente noti,**come`createElement('h1', {}, child1, child2, child3)`. Se i tuoi children sono dinamici, passa l'intero array come terzo argomento: `createElement('ul', {}, listItems)`. In questo modo React ti [avviserà delle `key` mancanti](/learn/rendering-lists#keeping-list-items-in-order-with-key)per le liste dinamiche. Per le liste statiche non è necessario, perché non vengono mai riordinate.
65
72
66
73
---
67
74
68
75
## Usage {/*usage*/}
69
76
70
-
### Creating an element without JSX {/*creating-an-element-without-jsx*/}
77
+
### Creare un elemento senza JSX {/*creating-an-element-without-jsx*/}
71
78
72
-
If you don't like [JSX](/learn/writing-markup-with-jsx)or can't use it in your project, you can use `createElement`as an alternative.
79
+
Se non ti piace il [JSX](/learn/writing-markup-with-jsx)o non puoi usarlo nel tuo progetto, puoi usare `createElement`come alternativa.
73
80
74
-
To create an element without JSX, call`createElement`with some <CodeStepstep={1}>type</CodeStep>, <CodeStepstep={2}>props</CodeStep>, and <CodeStepstep={3}>children</CodeStep>:
81
+
Per creare un elemento senza JSX, chiama`createElement`con un <CodeStepstep={1}>type</CodeStep>, delle <CodeStepstep={2}>props</CodeStep> e dei <CodeStepstep={3}>children</CodeStep>:
The <CodeStepstep={3}>children</CodeStep> are optional, and you can pass as many as you need (the example above has three children). This code will display a `<h1>`header with a greeting. For comparison, here is the same example rewritten with JSX:
97
+
I <CodeStepstep={3}>children</CodeStep> sono opzionali e puoi passarne quanti ne servono (l'esempio sopra ne ha tre). Questo codice mostrerà un'intestazione `<h1>`con un saluto. A titolo di confronto, ecco lo stesso esempio riscritto con JSX:
Here is a complete example written with`createElement`:
125
+
Ecco un esempio completo scritto con`createElement`:
119
126
120
127
<Sandpack>
121
128
@@ -149,7 +156,7 @@ export default function App() {
149
156
150
157
</Sandpack>
151
158
152
-
And here is the same example written using JSX:
159
+
Ed ecco lo stesso esempio scritto con JSX:
153
160
154
161
<Sandpack>
155
162
@@ -176,16 +183,16 @@ export default function App() {
176
183
177
184
</Sandpack>
178
185
179
-
Both coding styles are fine, so you can use whichever one you prefer for your project. The main benefit of using JSX compared to`createElement`is that it's easy to see which closing tag corresponds to which opening tag.
186
+
Entrambi gli stili di codice vanno bene, quindi puoi usare quello che preferisci per il tuo progetto. Il vantaggio principale di usare JSX rispetto a`createElement`è che è facile vedere quale tag di chiusura corrisponde a quale tag di apertura.
180
187
181
188
<DeepDive>
182
189
183
-
#### What is a React element, exactly? {/*what-is-a-react-element-exactly*/}
190
+
#### Cos'è esattamente un elemento React? {/*what-is-a-react-element-exactly*/}
184
191
185
-
An element is a lightweight description of a piece of the user interface. For example, both`<Greeting name="Taylor" />`and`createElement(Greeting, { name: 'Taylor' })`produce an object like this:
192
+
Un elemento è una descrizione leggera di una porzione dell'interfaccia utente. Ad esempio, sia`<Greeting name="Taylor" />`sia`createElement(Greeting, { name: 'Taylor' })`producono un oggetto come questo:
186
193
187
194
```js
188
-
//Slightly simplified
195
+
//Leggermente semplificato
189
196
{
190
197
type: Greeting,
191
198
props: {
@@ -196,10 +203,10 @@ An element is a lightweight description of a piece of the user interface. For ex
196
203
}
197
204
```
198
205
199
-
**Note that creating this object does not render the`Greeting`component or create any DOM elements.**
206
+
**Nota che creare questo oggetto non renderizza il componente`Greeting`né crea elementi DOM.**
200
207
201
-
A React element is more like a description--an instruction for React to later render the `Greeting` component. By returning this object from your`App` component, you tell React what to do next.
208
+
Un elemento React è più simile a una descrizione — un'istruzione per React di renderizzare in seguito il componente `Greeting`. Restituendo questo oggetto dal tuo componente`App`, indichi a React cosa fare dopo.
202
209
203
-
Creating elements is extremely cheap so you don't need to try to optimize or avoid it.
210
+
Creare elementi ha un costo estremamente basso, quindi non devi cercare di ottimizzarlo o evitarlo.
0 commit comments