Closed tjenkinson closed 10 months ago
A bit of a shame to not be able to batch requests anymore, that was one of the key differentiators of tiny-atom, but as you've clearly laid out - it can be buggy indeed! And React is now doing batching natively in more situations now. I'll cut a new breaking release.
React batches state updates, so multiple
rerender
state updates in the same frame won't trigger multiple rerenders. There will still only be one render, and it will start from the highest component that saw the state change and work down.The problem with
sync: false
is that other state changes in a component can cause it to rerender, before the RAF callback triggers the state update due to an atom change. The early rerenders would still see the latest atom state though, which means child components may see a newer version of an atom state before a parent.How does it break?
With the following
1.
user
is already set in the atomParent
containingChild
user
is set tonull
in the atomrerender
state updatesetCounter
being calledChild
. It doesn't renderParent
because no state changed aboveChild
id
onuser
givenuser
isnull
.useSelector
always returns the live atom stateNote that
requestAnimationFrame
calls are usually paused whilst a tab is in the background, so it's more likely for other state updates to happen in components during that time.If instead of the RAF call the
rerender
state was updated at that point, react would see thererender
state change for both components, and then trigger a single render fromParent
down.