Replace forwarded ref instances of useProvidedRefOrCreate with useMergedRefs#7644
Replace forwarded ref instances of useProvidedRefOrCreate with useMergedRefs#7644iansan5653 wants to merge 24 commits intomainfrom
useProvidedRefOrCreate with useMergedRefs#7644Conversation
|
|
422c4e4 to
006cc27
Compare
…se-provided-ref-or-create
|
🤖 Lint issues have been automatically fixed and committed to this PR. |
useProvidedRefOrCreate and migrate everything except for hooksuseProvidedRefOrCreate with useMergedRefs
| // Only works in React 19. In React 18, the cleanup function will be ignored and the ref will get called with | ||
| // Callback refs only work in React 19+. In React 18, the ref will get called with | ||
| // `null` which will be passed to each ref as expected. | ||
| if (majorReactVersion <= 18) return |
There was a problem hiding this comment.
I was getting a test failure because of an extra call to console.error in React 18 tests. Things are working as expected but React warns when a cleanup function is returned from a callback ref in versions before 19, because it's trying to future-proof for 19 and not cause unexpected side effects.
To resolve the warning, I just inspect the version of React explicitly instead of relying on the implicit differences in behavior. When we drop 19 support we can remove this.
There was a problem hiding this comment.
Shall we add a TODO:? We sometimes grep for those
hectahertz
left a comment
There was a problem hiding this comment.
This looks good! Let's get the integration tests passing and merge 🚀
|
👋 Hi from github/github-ui! Your integration PR is ready: https://github.com/github/github-ui/pull/17627 |
In #7638 I created a
useMergedRefshook and migrated all instances ofuseRefObjectAsForwardedRefto it.A similar pattern is using
useProvidedRefOrCreatefor this. It's typically used with a type cast and ats-expect-errorcomment (❗):This is obviously problematic. The type errors are trying to point out the problem with this pattern: it will break if consumers pass ref callbacks. Ref callbacks are likely to become more and more common as React 19 has introduced ref callback cleanup functions, making ref callbacks a viable alternative to effects.
A much better pattern is to be consistent and use the same approach as in #7638:
Note, however, that I did not deprecate
useProvidedRefOrCreateor remove all calls to it. There are still some valid use cases where refs can be passed in to refer to elements rendered outside the component. We most often see this in anchor refs. There are also some utility hooks that can create a ref internally or use a passed ref. These two cases are still valid; the case I'm targeting here is specifically around forwarded refs.useMergedRefsto be React 19 compatible and public + deprecateuseRefObjectAsForwardedRefanduseProvidedRefOrCreate#7672Changelog
New
Changed
Removed
Rollout strategy
Testing & Reviewing
Merge checklist