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/lazy.md
+33-26Lines changed: 33 additions & 26 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: lazy
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/lazy.md).
9
+
10
+
</Note>
11
+
5
12
<Intro>
6
13
7
-
`lazy`lets you defer loading component's code until it is rendered for the first time.
14
+
`lazy`ti permette di posticipare il caricamento del codice del componente fino alla sua prima renderizzazione.
*`load`: A function that returns a[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)or another *thenable* (a Promise-like object with a`then` method). React will not call `load`until the first time you attempt to render the returned component. After React first calls `load`, it will wait for it to resolve, and then render the resolved value's `.default`as a React component. Both the returned Promise and the Promise's resolved value will be cached, so React will not call `load`more than once. If the Promise rejects, React will `throw` the rejection reason for the nearest Error Boundary to handle.
42
+
*`load`: Una funzione che restituisce una[Promise](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Promise)o un altro *thenable* (un oggetto simile a una Promise con un metodo`then`). React non chiamerà `load`fino al primo tentativo di renderizzare il componente restituito. Dopo la prima chiamata a `load`, React attenderà che venga risolta e poi renderizzerà la proprietà `.default`del valore risolto come componente React. Sia la Promise restituita sia il valore risolto della Promise verranno memorizzati in cache, quindi React non chiamerà `load`più di una volta. Se la Promise viene rifiutata, React `throw`erà il motivo del rifiuto affinché lo gestisca il contenitore di errori più vicino.
36
43
37
44
#### Returns {/*returns*/}
38
45
39
-
`lazy`returns a React component you can render in your tree. While the code for the lazy component is still loading, attempting to render it will *suspend.*Use[`<Suspense>`](/reference/react/Suspense)to display a loading indicator while it's loading.
46
+
`lazy`restituisce un componente React che puoi renderizzare nel tuo albero. Mentre il codice del componente lazy è ancora in caricamento, il tentativo di renderizzarlo lo farà *andare in sospensione.*Usa[`<Suspense>`](/reference/react/Suspense)per mostrare un indicatore di caricamento mentre è in caricamento.
40
47
41
48
---
42
49
43
-
### `load` function {/*load*/}
50
+
### Funzione `load` {/*load*/}
44
51
45
52
#### Parameters {/*load-parameters*/}
46
53
47
-
`load`receives no parameters.
54
+
`load`non riceve parametri.
48
55
49
56
#### Returns {/*load-returns*/}
50
57
51
-
You need to return a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)or some other*thenable* (a Promise-like object with a`then` method). It needs to eventually resolve to an object whose`.default`property is a valid React component type, such as a function, [`memo`](/reference/react/memo), or a [`forwardRef`](/reference/react/forwardRef) component.
58
+
Devi restituire una [Promise](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Promise)o un altro*thenable* (un oggetto simile a una Promise con un metodo`then`). Deve risolversi infine in un oggetto la cui proprietà`.default`è un tipo componente React valido, come una funzione, un componente [`memo`](/reference/react/memo) o un componente [`forwardRef`](/reference/react/forwardRef).
52
59
53
60
---
54
61
55
62
## Usage {/*usage*/}
56
63
57
-
### Lazy-loading components with Suspense {/*suspense-for-code-splitting*/}
64
+
### Lazy-loading dei componenti con Suspense {/*suspense-for-code-splitting*/}
58
65
59
-
Usually, you import components with the static [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) declaration:
66
+
Di solito, importi i componenti con la dichiarazione statica [`import`](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Statements/import):
60
67
61
68
```js
62
69
importMarkdownPreviewfrom'./MarkdownPreview.js';
63
70
```
64
71
65
-
To defer loading this component's code until it's rendered for the first time, replace this import with:
72
+
Per posticipare il caricamento del codice di questo componente fino alla sua prima renderizzazione, sostituisci questa importazione con:
This code relies on [dynamic `import()`,](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import)which might require support from your bundler or framework. Using this pattern requires that the lazy component you're importing was exported as the`default` export.
80
+
Questo codice si basa su [`import()` dinamico,](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Operators/import)che potrebbe richiedere il supporto del tuo bundler o framework. Usare questo pattern richiede che il componente lazy che importi sia stato esportato come export`default`.
74
81
75
-
Now that your component's code loads on demand, you also need to specify what should be displayed while it is loading. You can do this by wrapping the lazy component or any of its parents into a[`<Suspense>`](/reference/react/Suspense) boundary:
82
+
Ora che il codice del tuo componente si carica on demand, devi anche specificare cosa mostrare mentre è in caricamento. Puoi farlo avvolgendo il componente lazy o uno dei suoi genitori in un boundary[`<Suspense>`](/reference/react/Suspense):
76
83
77
84
```js {1,4}
78
85
<Suspense fallback={<Loading />}>
79
-
<h2>Preview</h2>
86
+
<h2>Anteprima</h2>
80
87
<MarkdownPreview />
81
88
</Suspense>
82
89
```
83
90
84
-
In this example, the code for`MarkdownPreview`won't be loaded until you attempt to render it. If`MarkdownPreview`hasn't loaded yet, `Loading` will be shown in its place. Try ticking the checkbox:
91
+
In questo esempio, il codice di`MarkdownPreview`non verrà caricato finché non tenti di renderizzarlo. Se`MarkdownPreview`non si è ancora caricato, al suo posto verrà mostrato `Loading`. Prova a selezionare la checkbox:
85
92
86
93
<Sandpack>
87
94
@@ -99,20 +106,20 @@ export default function MarkdownEditor() {
//Add a fixed delay so you can see the loading state
122
+
//Aggiungi un ritardo fisso per poter vedere lo state di caricamento
116
123
functiondelayForDemo(promise) {
117
124
returnnewPromise(resolve=> {
118
125
setTimeout(resolve, 2000);
@@ -122,7 +129,7 @@ function delayForDemo(promise) {
122
129
123
130
```js src/Loading.js
124
131
exportdefaultfunctionLoading() {
125
-
return<p><i>Loading...</i></p>;
132
+
return<p><i>Caricamento...</i></p>;
126
133
}
127
134
```
128
135
@@ -175,34 +182,34 @@ body {
175
182
176
183
</Sandpack>
177
184
178
-
This demo loads with an artificial delay. The next time you untick and tick the checkbox, `Preview` will be cached, so there will be no loading state. To see the loading state again, click "Reset" on the sandbox.
185
+
Questa demo usa un ritardo artificiale. La prossima volta che deselezioni e selezioni di nuovo la checkbox, `Anteprima` sarà in cache, quindi non ci sarà alcuno state di caricamento. Per vedere di nuovo lo state di caricamento, fai clic su "Reset" nella sandbox.
179
186
180
-
[Learn more about managing loading states with Suspense.](/reference/react/Suspense)
187
+
[Scopri di più sulla gestione degli state di caricamento con Suspense.](/reference/react/Suspense)
181
188
182
189
---
183
190
184
191
## Troubleshooting {/*troubleshooting*/}
185
192
186
-
### My `lazy`component's state gets reset unexpectedly {/*my-lazy-components-state-gets-reset-unexpectedly*/}
193
+
### Lo state del mio componente `lazy`viene resettato in modo imprevisto {/*my-lazy-components-state-gets-reset-unexpectedly*/}
187
194
188
-
Do not declare`lazy`components *inside* other components:
195
+
Non dichiarare componenti`lazy`*all'interno* di altri componenti:
189
196
190
197
```js {4-5}
191
198
import { lazy } from'react';
192
199
193
200
functionEditor() {
194
-
// 🔴 Bad: This will cause all state to be reset on re-renders
201
+
// 🔴 Sbagliato: questo farà resettare tutto lo state alle ri-renderizzazioni
0 commit comments