@@ -36,14 +36,15 @@ Adding Interactivity
3636 :link: state-as-a-snapshot
3737 :link-type: doc
3838
39- Learn why IDOM does not change component state the moment it is set, but
40- instead schedules a re-render .
39+ Learn why state updates schedules a re-render, instead of being applied
40+ immediately .
4141
4242 .. grid-item-card :: :octicon:`versions` Multiple State Updates
4343 :link: multiple-state-updates
4444 :link-type: doc
4545
46- Under construction 🚧
46+ Learn how updates to a components state can be batched, or applied
47+ incrementally.
4748
4849 .. grid-item-card :: :octicon:`issue-opened` Dangers of Mutability
4950 :link: dangers-of-mutability
@@ -147,12 +148,31 @@ snapshot.
147148 :octicon: `book ` Read More
148149 ^^^^^^^^^^^^^^^^^^^^^^^^^
149150
150- Learn why IDOM does not change component state the moment it is set, but instead
151- schedules a re-render.
151+ Learn why state updates schedules a re-render, instead of being applied immediately.
152152
153153
154154Section 4: Multiple State Updates
155- ------------------------------------
155+ ---------------------------------
156+
157+ As we saw in an earlier example, :ref: `setting state triggers renders `. In other words,
158+ changes to state only take effect in the next render, not in the current one. Further,
159+ changes to state are batched, calling a particular state setter 3 times won't trigger 3
160+ renders, it will only trigger 1. This means that multiple state assignments are batched
161+ - so long as the event handler is synchronous (i.e. the event handler is not an
162+ ``async `` function), IDOM waits until all the code in an event handler has run before
163+ processing state and starting the next render:
164+
165+ .. idom :: _examples/set_color_3_times
166+
167+ Sometimes though, you need to update a state variable more than once before the next
168+ render. In these cases, instead of having updates batched, you instead want them to be
169+ applied incrementally. That is, the next update can be made to depend on the prior one.
170+ To accomplish this, instead of passing the next state value directly (e.g.
171+ ``set_state(new_state) ``), we may pass an **"updater function" ** of the form
172+ ``compute_new_state(old_state) `` to the state setter (e.g.
173+ ``set_state(compute_new_state) ``):
174+
175+ .. idom :: _examples/set_state_function
156176
157177.. card ::
158178 :link: multiple-state-updates
@@ -161,7 +181,7 @@ Section 4: Multiple State Updates
161181 :octicon: `book ` Read More
162182 ^^^^^^^^^^^^^^^^^^^^^^^^^
163183
164- .. .
184+ Learn how updates to a components state can be batched, or applied incrementally .
165185
166186
167187Section 5: Dangers of Mutability
0 commit comments