Skip to content

Commit 2e8328a

Browse files
deblasiscursoragent
andcommitted
translate(reference): createElement
Signed-off-by: Alessandro De Blasis <alex@deblasis.net> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 3c0e3b4 commit 2e8328a

1 file changed

Lines changed: 37 additions & 30 deletions

File tree

src/content/reference/react/createElement.md

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

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

916
```js
1017
const element = createElement(type, props, ...children)
@@ -20,7 +27,7 @@ const element = createElement(type, props, ...children)
2027

2128
### `createElement(type, props, ...children)` {/*createelement*/}
2229

23-
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.
2431

2532
```js
2633
import { createElement } from 'react';
@@ -34,44 +41,44 @@ function Greeting({ name }) {
3441
}
3542
```
3643

37-
[See more examples below.](#usage)
44+
[Vedi altri esempi sotto.](#usage)
3845

3946
#### Parameters {/*parameters*/}
4047

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)).
4249

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

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

4754
#### Returns {/*returns*/}
4855

49-
`createElement` returns a React element object with a few properties:
56+
`createElement` restituisce un oggetto elemento React con alcune proprietà:
5057

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

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

5865
#### Caveats {/*caveats*/}
5966

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

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).
6370

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

6673
---
6774

6875
## Usage {/*usage*/}
6976

70-
### Creating an element without JSX {/*creating-an-element-without-jsx*/}
77+
### Creare un elemento senza JSX {/*creating-an-element-without-jsx*/}
7178

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

74-
To create an element without JSX, call `createElement` with some <CodeStep step={1}>type</CodeStep>, <CodeStep step={2}>props</CodeStep>, and <CodeStep step={3}>children</CodeStep>:
81+
Per creare un elemento senza JSX, chiama `createElement` con un <CodeStep step={1}>type</CodeStep>, delle <CodeStep step={2}>props</CodeStep> e dei <CodeStep step={3}>children</CodeStep>:
7582

7683
```js [[1, 5, "'h1'"], [2, 6, "{ className: 'greeting' }"], [3, 7, "'Hello ',"], [3, 8, "createElement('i', null, name),"], [3, 9, "'. Welcome!'"]]
7784
import { createElement } from 'react';
@@ -87,7 +94,7 @@ function Greeting({ name }) {
8794
}
8895
```
8996

90-
The <CodeStep step={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 <CodeStep step={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:
9198

9299
```js [[1, 3, "h1"], [2, 3, "className=\\"greeting\\""], [3, 4, "Hello <i>{name}</i>. Welcome!"], [1, 5, "h1"]]
93100
function Greeting({ name }) {
@@ -99,23 +106,23 @@ function Greeting({ name }) {
99106
}
100107
```
101108

102-
To render your own React component, pass a function like `Greeting` as the <CodeStep step={1}>type</CodeStep> instead of a string like `'h1'`:
109+
Per renderizzare il tuo componente React, passa una funzione come `Greeting` come <CodeStep step={1}>type</CodeStep> invece di una stringa come `'h1'`:
103110

104111
```js [[1, 2, "Greeting"], [2, 2, "{ name: 'Taylor' }"]]
105112
export default function App() {
106113
return createElement(Greeting, { name: 'Taylor' });
107114
}
108115
```
109116

110-
With JSX, it would look like this:
117+
Con JSX, apparirebbe così:
111118

112119
```js [[1, 2, "Greeting"], [2, 2, "name=\\"Taylor\\""]]
113120
export default function App() {
114121
return <Greeting name="Taylor" />;
115122
}
116123
```
117124

118-
Here is a complete example written with `createElement`:
125+
Ecco un esempio completo scritto con `createElement`:
119126

120127
<Sandpack>
121128

@@ -149,7 +156,7 @@ export default function App() {
149156

150157
</Sandpack>
151158

152-
And here is the same example written using JSX:
159+
Ed ecco lo stesso esempio scritto con JSX:
153160

154161
<Sandpack>
155162

@@ -176,16 +183,16 @@ export default function App() {
176183

177184
</Sandpack>
178185

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

181188
<DeepDive>
182189

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*/}
184191

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:
186193

187194
```js
188-
// Slightly simplified
195+
// Leggermente semplificato
189196
{
190197
type: Greeting,
191198
props: {
@@ -196,10 +203,10 @@ An element is a lightweight description of a piece of the user interface. For ex
196203
}
197204
```
198205

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.**
200207

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

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

205212
</DeepDive>

0 commit comments

Comments
 (0)