-
Notifications
You must be signed in to change notification settings - Fork 450
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: use useImperativeHandle
instead of mutating a parent ref
#6813
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Ignored Deployment
|
New and removed dependencies detected. Learn more about Socket for GitHub ↗︎
🚮 Removed packages: npm/@sanity/[email protected], npm/@sanity/[email protected], npm/@sanity/[email protected], npm/@sanity/[email protected], npm/@sanity/[email protected], npm/@sanity/[email protected], npm/@sanity/[email protected], npm/@tanstack/[email protected], npm/@tanstack/[email protected], npm/@testing-library/[email protected], npm/@testing-library/[email protected], npm/@testing-library/[email protected], npm/[email protected] |
No changes to documentation |
Component Testing Report Updated May 30, 2024 2:50 PM (UTC)
|
[forwardedRef], | ||
) | ||
// Forward ref to parent | ||
useImperativeHandle(ref, () => rootElement!, [rootElement]) |
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.
This is pedantic, but would it be more correct to type the imperative handle as HTMLDivElement | null
, rather than using a non-null assertion? My understanding is that rootElement!
asserts rootElement
is never null
, but in reality, it is.
useImperativeHandle(ref, () => rootElement!, [rootElement]) | |
useImperativeHandle<HTMLDivElement | null, HTMLDivElement | null>(ref, () => rootElement, [ | |
rootElement, | |
]) |
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.
Yes, you're correct. The parent ref: ForwardedRef<HTMLDivElement>
mandates it's always assigned, so I considered changing it as well so it's fully consistent. But it caused other side-effects to the type checks that felt out of scope. Do you want me to do it in a follow-up?
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.
I wondered about that, too, but ForwardedRef
appears to always produce a nullable result (which is correct for consumers of this component).
IMO it's probably not worth spending more time on 😃.
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.
This looks good to me! I left one small comment, but it's not a blocker.
Description
React Compiler isn't able to optimize
<Pane>
when it mutates theforwardedRef
value returned byuseForwardedRef
.That's fine, because we can use
useImperativeHandle
to forward refs to the parent in a way that is sound, and that React Compiler is able to handle.We should probably deprecate the
useForwardedRef
hook in@sanity/ui
asuseImperativeHandle
is a much better alternative (useForwardedRef
for example needs auseEffect
cycle to handle forwarding):What to review
Does it make sense?
Testing
Existing tests should be sufficient, as the refactor doesn't have practical differences, beyond being slightly faster in general, and noticeably faster when React Compiler is enabled.
Notes for release
N/A