Why do we need batching in signals if React batches state updates? #501
-
Just stumbled upon the |
Beta Was this translation helpful? Give feedback.
Answered by
rschristian
Jan 23, 2024
Replies: 1 comment 1 reply
-
React can batch state updates, but there's no guarantees how or when they will. import { signal, computed, effect } from '@preact/signals-react';
const fName = signal('John');
const lName = signal('Smith');
const fullName = computed(() => `${fName.value} ${lName.value}`);
effect(() => console.log(fullName.value));
fName.value = 'Jane';
lName.value = 'Doe'; This could log:
Batching the name updates will always log this:
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
crabvk
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
React can batch state updates, but there's no guarantees how or when they will.
batch()
provides this guarantee.This could log:
Batching the name updates will always log this: