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-dom/flushSync.md
+44-37Lines changed: 44 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,23 @@
1
1
---
2
2
title: flushSync
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-dom/flushSync.md).
9
+
10
+
</Note>
11
+
5
12
<Pitfall>
6
13
7
-
Using`flushSync`is uncommon and can hurt the performance of your app.
14
+
Usare`flushSync`è poco comune e può compromettere le prestazioni della tua app.
8
15
9
16
</Pitfall>
10
17
11
18
<Intro>
12
19
13
-
`flushSync`lets you force React to flush any updates inside the provided callback synchronously. This ensures that the DOM is updated immediately.
20
+
`flushSync`ti permette di forzare React a svuotare in modo sincrono tutti gli aggiornamenti all'interno della callback fornita. In questo modo il DOM viene aggiornato immediatamente.
14
21
15
22
```js
16
23
flushSync(callback)
@@ -26,7 +33,7 @@ flushSync(callback)
26
33
27
34
### `flushSync(callback)` {/*flushsync*/}
28
35
29
-
Call`flushSync`to force React to flush any pending work and update the DOM synchronously.
36
+
Chiama`flushSync`per forzare React a svuotare qualsiasi lavoro in sospeso e ad aggiornare il DOM in modo sincrono.
30
37
31
38
```js
32
39
import { flushSync } from'react-dom';
@@ -36,50 +43,50 @@ flushSync(() => {
36
43
});
37
44
```
38
45
39
-
Most of the time, `flushSync`can be avoided. Use`flushSync`as last resort.
46
+
Nella maggior parte dei casi, `flushSync`può essere evitato. Usa`flushSync`come ultima risorsa.
40
47
41
-
[See more examples below.](#usage)
48
+
[Vedi altri esempi sotto.](#usage)
42
49
43
50
#### Parameters {/*parameters*/}
44
51
45
52
46
-
*`callback`: A function. React will immediately call this callback and flush any updates it contains synchronously. It may also flush any pending updates, or Effects, or updates inside of Effects. If an update suspends as a result of this `flushSync` call, the fallbacks may be re-shown.
53
+
*`callback`: Una funzione. React chiamerà immediatamente questa callback e svuoterà in modo sincrono tutti gli aggiornamenti che contiene. Può anche svuotare aggiornamenti in sospeso, o Effetti, o aggiornamenti all'interno di Effetti. Se un aggiornamento va in sospensione a causa di questa chiamata a `flushSync`, i fallback potrebbero essere mostrati di nuovo.
47
54
48
55
#### Returns {/*returns*/}
49
56
50
-
`flushSync`returns`undefined`.
57
+
`flushSync`restituisce`undefined`.
51
58
52
59
#### Caveats {/*caveats*/}
53
60
54
-
*`flushSync`can significantly hurt performance. Use sparingly.
55
-
*`flushSync`may force pending Suspense boundaries to show their `fallback` state.
56
-
*`flushSync`may run pending Effects and synchronously apply any updates they contain before returning.
57
-
*`flushSync`may flush updates outside the callback when necessary to flush the updates inside the callback. For example, if there are pending updates from a click, React may flush those before flushing the updates inside the callback.
61
+
*`flushSync`può compromettere significativamente le prestazioni. Usalo con parsimonia.
62
+
*`flushSync`può forzare i confini Suspense in sospeso a mostrare il `fallback`.
63
+
*`flushSync`può eseguire Effetti in sospeso e applicare in modo sincrono tutti gli aggiornamenti che contengono prima di restituire il controllo.
64
+
*`flushSync`può svuotare aggiornamenti al di fuori della callback quando necessario per svuotare gli aggiornamenti all'interno della callback. Ad esempio, se ci sono aggiornamenti in sospeso da un click, React potrebbe svuotarli prima di svuotare gli aggiornamenti all'interno della callback.
58
65
59
66
---
60
67
61
68
## Usage {/*usage*/}
62
69
63
-
### Flushing updates for third-party integrations {/*flushing-updates-for-third-party-integrations*/}
70
+
### Svuotare gli aggiornamenti per integrazioni con terze parti {/*flushing-updates-for-third-party-integrations*/}
64
71
65
-
When integrating with third-party code such as browser APIs or UI libraries, it may be necessary to force React to flush updates. Use`flushSync`to force React to flush any <CodeStepstep={1}>state updates</CodeStep> inside the callback synchronously:
72
+
Quando integri codice di terze parti come API del browser o librerie UI, potrebbe essere necessario forzare React a svuotare gli aggiornamenti. Usa`flushSync`per forzare React a svuotare in modo sincrono tutti gli <CodeStepstep={1}>aggiornamenti di state</CodeStep> all'interno della callback:
66
73
67
74
```js [[1, 2, "setSomething(123)"]]
68
75
flushSync(() => {
69
76
setSomething(123);
70
77
});
71
-
//By this line, the DOM is updated.
78
+
//A questo punto, il DOM è aggiornato.
72
79
```
73
80
74
-
This ensures that, by the time the next line of code runs, React has already updated the DOM.
81
+
In questo modo, quando viene eseguita la riga di codice successiva, React ha già aggiornato il DOM.
75
82
76
-
**Using`flushSync`is uncommon, and using it often can significantly hurt the performance of your app.**If your app only uses React APIs, and does not integrate with third-party libraries, `flushSync`should be unnecessary.
83
+
**Usare`flushSync`è poco comune, e usarlo spesso può compromettere significativamente le prestazioni della tua app.**Se la tua app usa solo API React e non si integra con librerie di terze parti, `flushSync`dovrebbe essere superfluo.
77
84
78
-
However, it can be helpful for integrating with third-party code like browser APIs.
85
+
Tuttavia, può essere utile per integrarsi con codice di terze parti come le API del browser.
79
86
80
-
Some browser APIs expect results inside of callbacks to be written to the DOM synchronously, by the end of the callback, so the browser can do something with the rendered DOM. In most cases, React handles this for you automatically. But in some cases it may be necessary to force a synchronous update.
87
+
Alcune API del browser si aspettano che i risultati all'interno delle callback vengano scritti nel DOM in modo sincrono, entro la fine della callback, così che il browser possa fare qualcosa con il DOM renderizzato. Nella maggior parte dei casi, React gestisce questo automaticamente. Ma in alcuni casi potrebbe essere necessario forzare un aggiornamento sincrono.
81
88
82
-
For example, the browser `onbeforeprint`API allows you to change the page immediately before the print dialog opens. This is useful for applying custom print styles that allow the document to display better for printing. In the example below, you use `flushSync`inside of the`onbeforeprint`callback to immediately "flush" the React state to the DOM. Then, by the time the print dialog opens, `isPrinting`displays "yes":
89
+
Ad esempio, l'API `onbeforeprint`del browser ti permette di modificare la pagina immediatamente prima che si apra la finestra di dialogo di stampa. È utile per applicare stili di stampa personalizzati che permettono al documento di essere visualizzato meglio in stampa. Nell'esempio qui sotto, usi `flushSync`all'interno della callback`onbeforeprint`per "svuotare" immediatamente lo state React nel DOM. Così, quando si apre la finestra di dialogo di stampa, `isPrinting`mostra "yes":
83
90
84
91
<Sandpack>
85
92
@@ -113,7 +120,7 @@ export default function PrintApp() {
113
120
<>
114
121
<h1>isPrinting: {isPrinting ?'yes':'no'}</h1>
115
122
<button onClick={() =>window.print()}>
116
-
Print
123
+
Stampa
117
124
</button>
118
125
</>
119
126
);
@@ -122,46 +129,46 @@ export default function PrintApp() {
122
129
123
130
</Sandpack>
124
131
125
-
Without`flushSync`, the print dialog will display `isPrinting`as "no". This is because React batches the updates asynchronously and the print dialog is displayed before the state is updated.
132
+
Senza`flushSync`, la finestra di dialogo di stampa mostrerà `isPrinting`come "no". Questo perché React raggruppa gli aggiornamenti in modo asincrono e la finestra di dialogo di stampa viene visualizzata prima che lo state venga aggiornato.
126
133
127
134
<Pitfall>
128
135
129
-
`flushSync`can significantly hurt performance, and may unexpectedly force pending Suspense boundaries to show their fallback state.
136
+
`flushSync`può compromettere significativamente le prestazioni e può forzare in modo imprevisto i confini Suspense in sospeso a mostrare il `fallback`.
130
137
131
-
Most of the time, `flushSync`can be avoided, so use `flushSync` as a last resort.
138
+
Nella maggior parte dei casi, `flushSync`può essere evitato, quindi usalo come ultima risorsa.
132
139
133
140
</Pitfall>
134
141
135
142
---
136
143
137
144
## Troubleshooting {/*troubleshooting*/}
138
145
139
-
### I'm getting an error: "flushSync was called from inside a lifecycle method" {/*im-getting-an-error-flushsync-was-called-from-inside-a-lifecycle-method*/}
146
+
### Ricevo un errore: "flushSync was called from inside a lifecycle method" {/*im-getting-an-error-flushsync-was-called-from-inside-a-lifecycle-method*/}
140
147
141
148
142
-
React cannot `flushSync`in the middle of a render. If you do, it will noop and warn:
149
+
React non può eseguire `flushSync`nel mezzo di una renderizzazione. Se lo fai, non avrà effetto e mostrerà un avviso:
143
150
144
151
<ConsoleBlocklevel="error">
145
152
146
153
Warning: flushSync was called from inside a lifecycle method. React cannot flush when React is already rendering. Consider moving this call to a scheduler task or micro task.
147
154
148
155
</ConsoleBlock>
149
156
150
-
This includes calling `flushSync`inside:
157
+
Questo include la chiamata a `flushSync`all'interno di:
151
158
152
-
-rendering a component.
153
-
-`useLayoutEffect`or`useEffect` hooks.
154
-
-Class component lifecycle methods.
159
+
-la renderizzazione di un componente.
160
+
-gli hooks `useLayoutEffect`o`useEffect`.
161
+
-i metodi lifecycle dei componenti classe.
155
162
156
-
For example, calling`flushSync` in an Effect will noop and warn:
163
+
Ad esempio, chiamare`flushSync` in un Effetto non avrà effetto e mostrerà un avviso:
157
164
158
165
```js
159
166
import { useEffect } from'react';
160
167
import { flushSync } from'react-dom';
161
168
162
169
functionMyComponent() {
163
170
useEffect(() => {
164
-
// 🚩 Wrong: calling flushSync inside an effect
171
+
// 🚩 Sbagliato: chiamare flushSync all'interno di un Effetto
165
172
flushSync(() => {
166
173
setSomething(newValue);
167
174
});
@@ -171,23 +178,23 @@ function MyComponent() {
171
178
}
172
179
```
173
180
174
-
To fix this, you usually want to move the `flushSync`call to an event:
181
+
Per risolvere, di solito vuoi spostare la chiamata a `flushSync`in un evento:
175
182
176
183
```js
177
184
functionhandleClick() {
178
-
// ✅ Correct: flushSync in event handlers is safe
185
+
// ✅ Corretto: flushSync nei gestori di eventi è sicuro
179
186
flushSync(() => {
180
187
setSomething(newValue);
181
188
});
182
189
}
183
190
```
184
191
185
192
186
-
If it's difficult to move to an event, you can defer `flushSync` in a microtask:
193
+
Se è difficile spostarlo in un evento, puoi rimandare `flushSync` in una microtask:
187
194
188
195
```js {3,7}
189
196
useEffect(() => {
190
-
// ✅ Correct: defer flushSync to a microtask
197
+
// ✅ Corretto: rimandare flushSync a una microtask
191
198
queueMicrotask(() => {
192
199
flushSync(() => {
193
200
setSomething(newValue);
@@ -196,10 +203,10 @@ useEffect(() => {
196
203
}, []);
197
204
```
198
205
199
-
This will allow the current render to finish and schedule another syncronous render to flush the updates.
206
+
Questo permetterà alla renderizzazione corrente di completarsi e programmerà un'altra renderizzazione sincrona per svuotare gli aggiornamenti.
200
207
201
208
<Pitfall>
202
209
203
-
`flushSync`can significantly hurt performance, but this particular pattern is even worse for performance. Exhaust all other options before calling `flushSync` in a microtask as an escape hatch.
210
+
`flushSync`può compromettere significativamente le prestazioni, ma questo particolare pattern è ancora peggio per le prestazioni. Esaurisci tutte le altre opzioni prima di chiamare `flushSync` in una microtask come escape hatch.
0 commit comments