25
loading...
This website collects cookies to deliver better user experience
From Michel Westrate's Becoming Fully Reactive: An in-depth explanation of MobX
count
a state doubleCount
would always be in fact double that count. In a reactive library we often refer to this as glitch-free
execution. It might look something like this:const [count, setCount] = useState(1);
const doubleCount = useMemo(() => count * 2, [count]);
// React
setCount(20);
console.log(count, doubleCount); // 1, 2
// Solid
setCount(20);
console.log(count, doubleCount); // 20, 40
count
and doubleCount
again but pretend we need to go to the server to calculate doubleCount
.// Not real React code, just for illustrative purposes
const [count, setCount] = useState(1);
const doubleCount = useMemo(async () =>
await fetchDoubleCount(count)
, [count]
);
// somewhere else:
setCount(20);
count
would start at 1 and doubleCount
would initially be undefined while it was fetching putting us in an inconsistent state. At some point later when it resolved doubleCount
would be 2 and we would be consistent again. This happens later when we set count
to 20. doubleCount
would be value 1 until it settled at 40. If you were logging this in a useEffect
you might see:1, undefined
1, 2
20, 1
20, 40
count
, and futureCount
and have doubleCount
be derived from futureCount
instead and only apply futureCount
's value back to count
when everything has resolved. But this gets tricky. What if there are more than one thing fetching and multiple different sources. We'd need to clone everything downstream of that change.setState
to stay in the past doesn't look so unusual. You don't know what might cause asynchronous derived state downstream so you would need to hedge on the side of not updating until you know. That being said these frameworks still have explicit opt-in to concurrent rendering for the same reason.