Skip to content

Commit 383c2c8

Browse files
committed
translate(reference): lazy
Signed-off-by: Alessandro De Blasis <alex@deblasis.net>
1 parent 5b06f67 commit 383c2c8

1 file changed

Lines changed: 33 additions & 26 deletions

File tree

‎src/content/reference/react/lazy.md‎

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

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

916
```js
1017
const SomeComponent = lazy(load)
@@ -20,68 +27,68 @@ const SomeComponent = lazy(load)
2027

2128
### `lazy(load)` {/*lazy*/}
2229

23-
Call `lazy` outside your components to declare a lazy-loaded React component:
30+
Chiama `lazy` fuori dai tuoi componenti per dichiarare un componente React caricato in lazy:
2431

2532
```js
2633
import { lazy } from 'react';
2734

2835
const MarkdownPreview = lazy(() => import('./MarkdownPreview.js'));
2936
```
3037

31-
[See more examples below.](#usage)
38+
[Vedi altri esempi sotto.](#usage)
3239

3340
#### Parameters {/*parameters*/}
3441

35-
* `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.
3643

3744
#### Returns {/*returns*/}
3845

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

4148
---
4249

43-
### `load` function {/*load*/}
50+
### Funzione `load` {/*load*/}
4451

4552
#### Parameters {/*load-parameters*/}
4653

47-
`load` receives no parameters.
54+
`load` non riceve parametri.
4855

4956
#### Returns {/*load-returns*/}
5057

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

5360
---
5461

5562
## Usage {/*usage*/}
5663

57-
### Lazy-loading components with Suspense {/*suspense-for-code-splitting*/}
64+
### Lazy-loading dei componenti con Suspense {/*suspense-for-code-splitting*/}
5865

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):
6067

6168
```js
6269
import MarkdownPreview from './MarkdownPreview.js';
6370
```
6471

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

6774
```js
6875
import { lazy } from 'react';
6976

7077
const MarkdownPreview = lazy(() => import('./MarkdownPreview.js'));
7178
```
7279

73-
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`.
7481

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):
7683

7784
```js {1,4}
7885
<Suspense fallback={<Loading />}>
79-
<h2>Preview</h2>
86+
<h2>Anteprima</h2>
8087
<MarkdownPreview />
8188
</Suspense>
8289
```
8390

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

8693
<Sandpack>
8794

@@ -99,20 +106,20 @@ export default function MarkdownEditor() {
99106
<textarea value={markdown} onChange={e => setMarkdown(e.target.value)} />
100107
<label>
101108
<input type="checkbox" checked={showPreview} onChange={e => setShowPreview(e.target.checked)} />
102-
Show preview
109+
Mostra anteprima
103110
</label>
104111
<hr />
105112
{showPreview && (
106113
<Suspense fallback={<Loading />}>
107-
<h2>Preview</h2>
114+
<h2>Anteprima</h2>
108115
<MarkdownPreview markdown={markdown} />
109116
</Suspense>
110117
)}
111118
</>
112119
);
113120
}
114121

115-
// Add a fixed delay so you can see the loading state
122+
// Aggiungi un ritardo fisso per poter vedere lo state di caricamento
116123
function delayForDemo(promise) {
117124
return new Promise(resolve => {
118125
setTimeout(resolve, 2000);
@@ -122,7 +129,7 @@ function delayForDemo(promise) {
122129

123130
```js src/Loading.js
124131
export default function Loading() {
125-
return <p><i>Loading...</i></p>;
132+
return <p><i>Caricamento...</i></p>;
126133
}
127134
```
128135

@@ -175,34 +182,34 @@ body {
175182

176183
</Sandpack>
177184

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

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

182189
---
183190

184191
## Troubleshooting {/*troubleshooting*/}
185192

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

188-
Do not declare `lazy` components *inside* other components:
195+
Non dichiarare componenti `lazy` *all'interno* di altri componenti:
189196

190197
```js {4-5}
191198
import { lazy } from 'react';
192199

193200
function Editor() {
194-
// 🔴 Bad: This will cause all state to be reset on re-renders
201+
// 🔴 Sbagliato: questo farà resettare tutto lo state alle ri-renderizzazioni
195202
const MarkdownPreview = lazy(() => import('./MarkdownPreview.js'));
196203
// ...
197204
}
198205
```
199206

200-
Instead, always declare them at the top level of your module:
207+
Dichiarali invece sempre al top level del tuo modulo:
201208

202209
```js {3-4}
203210
import { lazy } from 'react';
204211

205-
// ✅ Good: Declare lazy components outside of your components
212+
// ✅ Corretto: dichiara i componenti lazy fuori dai tuoi componenti
206213
const MarkdownPreview = lazy(() => import('./MarkdownPreview.js'));
207214

208215
function Editor() {

0 commit comments

Comments
 (0)