-
Notifications
You must be signed in to change notification settings - Fork 446
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: speed up hovered field provider #8044
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Skipped Deployment
|
No changes to documentation |
Component Testing Report Updated Dec 16, 2024 3:55 PM (UTC) ❌ Failed Tests (2) -- expand for details
|
⚡️ Editor Performance ReportUpdated Mon, 16 Dec 2024 15:50:56 GMT
Detailed information🏠 Reference resultThe performance result of
🧪 Experiment resultThe performance result of this branch
📚 Glossary
|
store: hoveredStore, | ||
} = useHoveredField() | ||
/** | ||
* The `useSyncExternalStore` has a super power: if the value returned by the snapshot hasn't changed since last time, React won't re-render the component. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤯
9d075f4
to
f5cd7df
Compare
ff6d4b8
to
a5a3ea5
Compare
Description
Every time a field is hovered, every field rerenders, even though each field is only interested if its hover state is
true
orfalse
:hover.bad.mov
This is due to the context provider broadcasting the current field that has focus, to all its
useContext
subscribers. Even though each field only wants to know a boolean, they rerender even if the boolean haven't changed.By refactoring to a simple
useSyncExternalStore
, we're able to usegetSnapshot
to control when renders happen. SincegetSnapshot
returns a boolean, it'll only re-render when the returned boolean actually changes. In other words, even ifgetSnapshot
is called every time a field is hovered, the components subscribed to the store won't rerender unless the boolean it cares about has changed.hover.good.mov
What to review
Are the code comments and this PR enough to explain this pattern?
Testing
Existing tests are sufficient.
Notes for release
Improved render performance when hovering fields, no more everything renders all at once on every hover 😮💨