-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: optimize useAtomSelector
for React 19
#106
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@affects atoms, react
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
useAtomSelector
has had to jump through so many hoops to support React 18. Some of these hoops have caused such ugly code that edge cases are possible. We've seen one that we can't reproduce consistently, but it comes down to React schedulinguseEffect
microtasks for a newly mounted component tree at different points in time that interweave withuseAtomSelector
'squeueMicrotask
calls inside itsuseEffect
s in that same component tree.useAtomSelector
has to support React StrictMode (and concurrent mode and everything StrictMode prepares for) while handling inline selectors, selectors that return referentially unstable results, and of course the old problematic React behavior of unmounting a component tree after the new tree has rendered, but before its effects have run.This is fairly difficult. But two underlying React issues in StrictMode (and only StrictMode - concurrent mode is fine) made it just ridiculous:
useRef
(anduseState()[1]
function) references are unstable due to StrictMode unnecessarily destroying those resources after initial render.useId
returns a different id after initial render.These made tracking down selector cache entries created for inline selectors on that initial render an impossible task to do perfectly. We had to put some hacky workarounds together to prevent memory leaks. These hacky workarounds included the problematic
queueMicrotask
in auseEffect
that reads data off the Zedux ecosystem (something that does persist between renders) to try to clean up leaked selector caches from inline selectors whose references changed on the second render. Anyway, it was a nightmare. But:This nightmare is no more! React 19 fixes both these underlying issues - refs are stable even in StrictMode and
useId
returns a consistent id even on initial render.So clean up
useAtomSelector
a ton. Get rid of theignorePhase
state machine, thequeueMicrotask
s,ecosystem.selectors._storage
, and the deferring graph edge creation until theuseEffect
runs. Go back to the good old ref swapping for inline selectors to prevent unnecessary graph updates. Update tests and double check all of them.What This Means for You
We're releasing this change under Zedux
v1.3.0-rc.x
. This change is compatible with React 18 non-StrictMode only.npm i @zedux/react@canary
.useMemo
) touseAtomSelector
. Either ensure you never do that or (recommended) upgrade to React 19 (which should be released soon) before upgrading to Zedux v1.3.