Skip to content

Commit bad1910

Browse files
committed
Section "Queueing a Series of State Updates": await in event handler cause immediate rerender
Signed-off-by: jzz <[email protected]>
1 parent d396238 commit bad1910

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"debounce": "^1.2.1",
3535
"github-slugger": "^1.3.0",
3636
"next": "^13.4.1",
37-
"next-remote-watch": "1.0.0",
37+
"next-remote-watch": "^1.0.0",
3838
"parse-numeric-range": "^1.2.0",
3939
"react": "^0.0.0-experimental-16d053d59-20230506",
4040
"react-collapsed": "4.0.4",

src/content/learn/queueing-a-series-of-state-updates.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,19 @@ But there is one other factor at play here. **React waits until *all* code in th
6767
> before the handler completes.
6868
>
6969
> - This behavior differs from synchronous event handlers, where React waits until the entire handler
70-
> finishes.
70+
> finishes.
7171
>
7272
> - Any state updates after the `await` will be treated as a separate batch,potentially causing multiple
73-
> renders.
73+
> renders.
7474
>
75-
> To avoid this behavior, keep all state updates within a single synchronous batch, or avoid using `await`
76-
> inside event handlers when possible.
75+
> To avoid this behavior, keep all state updates within a single synchronous
76+
> batch, or avoid using `await` inside event handlers when possible.
7777
7878
---
7979

8080

81+
-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.
82+
8183
<Illustration src="/images/docs/illustrations/i_react-batching.png" alt="An elegant cursor at a restaurant places and order multiple times with React, playing the part of the waiter. After she calls setState() multiple times, the waiter writes down the last one she requested as her final order." />
8284

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

0 commit comments

Comments
 (0)