From b442af4b0a29f4dda799a6da8ab2a3b0a42aac9b Mon Sep 17 00:00:00 2001 From: Alessandro De Blasis Date: Mon, 14 Sep 2026 07:56:52 +0300 Subject: [PATCH 1/2] translate(learn): queueing a series of state updates Signed-off-by: Alessandro De Blasis --- .../queueing-a-series-of-state-updates.md | 157 +++++++++--------- src/sidebarLearn.json | 2 +- 2 files changed, 83 insertions(+), 76 deletions(-) diff --git a/src/content/learn/queueing-a-series-of-state-updates.md b/src/content/learn/queueing-a-series-of-state-updates.md index b3be278cf..e42c52ea2 100644 --- a/src/content/learn/queueing-a-series-of-state-updates.md +++ b/src/content/learn/queueing-a-series-of-state-updates.md @@ -1,23 +1,30 @@ --- -title: Queueing a Series of State Updates +title: Accodare più aggiornamenti dello state +translationStatus: ai-draft --- + + +Questa pagina è stata tradotta automaticamente e potrebbe beneficiare di una revisione umana. [Migliora questa traduzione](https://github.com/reactjs/it.react.dev/edit/main/src/content/learn/queueing-a-series-of-state-updates.md). + + + -Setting a state variable will queue another render. But sometimes you might want to perform multiple operations on the value before queueing the next render. To do this, it helps to understand how React batches state updates. +Impostare una variabile di state metterà in coda un'altra renderizzazione. Ma a volte potresti voler eseguire più operazioni sul valore prima di mettere in coda la prossima renderizzazione. Per fare questo, aiuta capire come React raggruppa gli aggiornamenti dello state. -* What "batching" is and how React uses it to process multiple state updates -* How to apply several updates to the same state variable in a row +* Cosa sia il *raggruppamento* e come React lo usa per elaborare più aggiornamenti dello state +* Come applicare diversi aggiornamenti alla stessa variabile di state di seguito -## React batches state updates {/*react-batches-state-updates*/} +## React raggruppa gli aggiornamenti dello state {/*react-batches-state-updates*/} -You might expect that clicking the "+3" button will increment the counter three times because it calls `setNumber(number + 1)` three times: +Potresti aspettarti che cliccando il pulsante "+3" il contatore venga incrementato tre volte perché chiama `setNumber(number + 1)` tre volte: @@ -47,7 +54,7 @@ h1 { display: inline-block; margin: 10px; width: 30px; text-align: center; } -However, as you might recall from the previous section, [each render's state values are fixed](/learn/state-as-a-snapshot#rendering-takes-a-snapshot-in-time), so the value of `number` inside the first render's event handler is always `0`, no matter how many times you call `setNumber(1)`: +Tuttavia, come potresti ricordare dalla sezione precedente, [i valori dello state di ogni renderizzazione sono fissi](/learn/state-as-a-snapshot#rendering-takes-a-snapshot-in-time), quindi il valore di `number` all'interno del gestore di eventi della prima renderizzazione è sempre `0`, indipendentemente da quante volte chiami `setNumber(1)`: ```js setNumber(0 + 1); @@ -55,21 +62,21 @@ setNumber(0 + 1); setNumber(0 + 1); ``` -But there is one other factor at play here. **React waits until *all* code in the event handlers has run before processing your state updates.** This is why the re-render only happens *after* all these `setNumber()` calls. +Ma c'è un altro fattore in gioco. **React attende che *tutto* il codice nei gestori di eventi sia stato eseguito prima di elaborare gli aggiornamenti dello state.** Ecco perché la ri-renderizzazione avviene solo *dopo* tutte queste chiamate a `setNumber()`. -This might remind you of a waiter taking an order at the restaurant. A waiter doesn't run to the kitchen at the mention of your first dish! Instead, they let you finish your order, let you make changes to it, and even take orders from other people at the table. +Questo potrebbe ricordarti un cameriere che prende un ordine al ristorante. Un cameriere non corre in cucina alla menzione del tuo primo piatto! Invece, ti lascia finire l'ordine, ti permette di modificarlo e persino di prendere ordini da altre persone al tavolo. - + -This lets you update multiple state variables--even from multiple components--without triggering too many [re-renders.](/learn/render-and-commit#re-renders-when-state-updates) But this also means that the UI won't be updated until _after_ your event handler, and any code in it, completes. This behavior, also known as **batching,** makes your React app run much faster. It also avoids dealing with confusing "half-finished" renders where only some of the variables have been updated. +Questo ti permette di aggiornare più variabili di state — persino da più componenti — senza innescare troppe [ri-renderizzazioni.](/learn/render-and-commit#re-renders-when-state-updates) Ma significa anche che l'UI non verrà aggiornata finché il tuo gestore di eventi, e qualsiasi codice al suo interno, non sarà completato. Questo comportamento, noto anche come **raggruppamento,** fa sì che la tua app React sia molto più veloce. Evita anche di dover gestire renderizzazioni "a metà" confuse, in cui solo alcune variabili sono state aggiornate. -**React does not batch across *multiple* intentional events like clicks**--each click is handled separately. Rest assured that React only does batching when it's generally safe to do. This ensures that, for example, if the first button click disables a form, the second click would not submit it again. +**React non raggruppa tra *più* eventi intenzionali come i click** — ogni click viene gestito separatamente. Stai tranquillo: React raggruppa solo quando è generalmente sicuro farlo. Questo garantisce che, ad esempio, se il primo click disabilita un form, il secondo click non lo invii di nuovo. -## Updating the same state multiple times before the next render {/*updating-the-same-state-multiple-times-before-the-next-render*/} +## Aggiornare lo stesso state più volte prima della prossima renderizzazione {/*updating-the-same-state-multiple-times-before-the-next-render*/} -It is an uncommon use case, but if you would like to update the same state variable multiple times before the next render, instead of passing the *next state value* like `setNumber(number + 1)`, you can pass a *function* that calculates the next state based on the previous one in the queue, like `setNumber(n => n + 1)`. It is a way to tell React to "do something with the state value" instead of just replacing it. +È un caso d'uso poco comune, ma se vuoi aggiornare la stessa variabile di state più volte prima della prossima renderizzazione, invece di passare il *prossimo valore dello state* come `setNumber(number + 1)`, puoi passare una *funzione* che calcola il prossimo state in base al precedente nella coda, come `setNumber(n => n + 1)`. È un modo per dire a React di "fare qualcosa con il valore dello state" invece di sostituirlo semplicemente. -Try incrementing the counter now: +Prova ad incrementare il contatore adesso: @@ -99,10 +106,10 @@ h1 { display: inline-block; margin: 10px; width: 30px; text-align: center; } -Here, `n => n + 1` is called an **updater function.** When you pass it to a state setter: +Qui, `n => n + 1` è chiamata una **funzione di aggiornamento.** Quando la passi a un setter dello state: -1. React queues this function to be processed after all the other code in the event handler has run. -2. During the next render, React goes through the queue and gives you the final updated state. +1. React mette in coda questa funzione per essere elaborata dopo che tutto il resto del codice nel gestore di eventi è stato eseguito. +2. Durante la prossima renderizzazione, React attraversa la coda e ti restituisce lo state aggiornato finale. ```js setNumber(n => n + 1); @@ -110,26 +117,26 @@ setNumber(n => n + 1); setNumber(n => n + 1); ``` -Here's how React works through these lines of code while executing the event handler: +Ecco come React elabora queste righe di codice mentre esegue il gestore di eventi: -1. `setNumber(n => n + 1)`: `n => n + 1` is a function. React adds it to a queue. -1. `setNumber(n => n + 1)`: `n => n + 1` is a function. React adds it to a queue. -1. `setNumber(n => n + 1)`: `n => n + 1` is a function. React adds it to a queue. +1. `setNumber(n => n + 1)`: `n => n + 1` è una funzione. React la aggiunge a una coda. +1. `setNumber(n => n + 1)`: `n => n + 1` è una funzione. React la aggiunge a una coda. +1. `setNumber(n => n + 1)`: `n => n + 1` è una funzione. React la aggiunge a una coda. -When you call `useState` during the next render, React goes through the queue. The previous `number` state was `0`, so that's what React passes to the first updater function as the `n` argument. Then React takes the return value of your previous updater function and passes it to the next updater as `n`, and so on: +Quando chiami `useState` durante la prossima renderizzazione, React attraversa la coda. Il valore precedente di `number` era `0`, quindi è quello che React passa alla prima funzione di aggiornamento come argomento `n`. Poi React prende il valore restituito dalla funzione di aggiornamento precedente e lo passa alla successiva come `n`, e così via: -| queued update | `n` | returns | +| aggiornamento in coda | `n` | restituisce | |--------------|---------|-----| | `n => n + 1` | `0` | `0 + 1 = 1` | | `n => n + 1` | `1` | `1 + 1 = 2` | | `n => n + 1` | `2` | `2 + 1 = 3` | -React stores `3` as the final result and returns it from `useState`. +React memorizza `3` come risultato finale e lo restituisce da `useState`. -This is why clicking "+3" in the above example correctly increments the value by 3. -### What happens if you update state after replacing it {/*what-happens-if-you-update-state-after-replacing-it*/} +Ecco perché cliccare "+3" nell'esempio sopra incrementa correttamente il valore di 3. +### Cosa succede se aggiorni lo state dopo averlo sostituito {/*what-happens-if-you-update-state-after-replacing-it*/} -What about this event handler? What do you think `number` will be in the next render? +Che dire di questo gestore di eventi? Quale pensi che sarà il valore di `number` nella prossima renderizzazione? ```js