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/learn/queueing-a-series-of-state-updates.md
+9-8Lines changed: 9 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -63,20 +63,21 @@ But there is one other factor at play here. **React waits until *all* code in th
63
63
64
64
> **Note:** When using `await` inside an event handler:
65
65
>
66
-
> - React processes all state updates made before the `await` immediately,
67
-
> which triggers a rerender before the handler completes.
66
+
> - React processes all state updates made before the `await` immediately,which triggers a rerender
67
+
> before the handler completes.
68
68
>
69
-
> - This behavior differs from synchronous event handlers, where React
70
-
> waits until the entire handler finishes.
69
+
> - This behavior differs from synchronous event handlers, where React waits until the entire handler
70
+
> finishes.
71
71
>
72
-
> - Any state updates after the `await` will be treated as a separate batch,
73
-
> potentially causing multiple renders.
72
+
> - Any state updates after the `await` will be treated as a separate batch,potentially causing multiple
73
+
> renders.
74
74
>
75
-
> To avoid this behavior, keep all state updates within a single synchronous
76
-
> batch, or avoid using `await` inside event handlers when possible.
75
+
> To avoid this behavior, keep all state updates within a single synchronous batch, or avoid using `await`
76
+
> inside event handlers when possible.
77
77
78
78
---
79
79
80
+
80
81
<Illustrationsrc="/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." />
81
82
82
83
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