Skip to content

Replace forwarded ref instances of useProvidedRefOrCreate with useMergedRefs#7644

Open
iansan5653 wants to merge 24 commits intomainfrom
replace-use-provided-ref-or-create
Open

Replace forwarded ref instances of useProvidedRefOrCreate with useMergedRefs#7644
iansan5653 wants to merge 24 commits intomainfrom
replace-use-provided-ref-or-create

Conversation

@iansan5653
Copy link
Copy Markdown
Contributor

@iansan5653 iansan5653 commented Mar 9, 2026

In #7638 I created a useMergedRefs hook and migrated all instances of useRefObjectAsForwardedRef to it.

A similar pattern is using useProvidedRefOrCreate for this. It's typically used with a type cast and a ts-expect-error comment (❗):

const ref = useProvidedRefOrCreate(forwardedRef as RefObject<HTMLElement | null>)

// @ts-expect-error
return <div ref={ref} />

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:

const ref = useRef<HTMLElement>(null)
const combinedRef = useCombinedRefs(ref, forwardedRef)

return <div ref={combinedRef} />

Note, however, that I did not deprecate useProvidedRefOrCreate or 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.

Changelog

New

Changed

Removed

Rollout strategy

  • Patch release
  • Minor release
  • Major release; if selected, include a written rollout or migration plan
  • None; if selected, include a brief description as to why

Testing & Reviewing

Merge checklist

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 9, 2026

⚠️ No Changeset found

Latest commit: a86ba2b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added the integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm label Mar 9, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 9, 2026

⚠️ Action required

👋 Hi, this pull request contains changes to the source code that github/github-ui depends on. If you are GitHub staff, test these changes with github/github-ui using the integration workflow. Or, apply the integration-tests: skipped manually label to skip these checks.

@github-actions github-actions bot requested a deployment to storybook-preview-7644 March 9, 2026 15:33 Abandoned
@github-actions github-actions bot temporarily deployed to storybook-preview-7644 March 9, 2026 15:43 Inactive
@github-actions github-actions bot requested a deployment to storybook-preview-7644 March 9, 2026 15:51 Abandoned
@github-actions github-actions bot temporarily deployed to storybook-preview-7644 March 9, 2026 16:01 Inactive
@iansan5653 iansan5653 force-pushed the replace-use-provided-ref-or-create branch from 422c4e4 to 006cc27 Compare March 17, 2026 17:01
@iansan5653 iansan5653 changed the base branch from create-use-combined-refs to update-use-merged-refs March 17, 2026 17:01
Base automatically changed from update-use-merged-refs to main March 18, 2026 19:40
@github-actions github-actions bot requested a deployment to storybook-preview-7644 March 24, 2026 18:55 Abandoned
@primer
Copy link
Copy Markdown
Contributor

primer bot commented Mar 24, 2026

🤖 Lint issues have been automatically fixed and committed to this PR.

@github-actions github-actions bot temporarily deployed to storybook-preview-7644 March 24, 2026 19:13 Inactive
@iansan5653 iansan5653 changed the title Deprecate useProvidedRefOrCreate and migrate everything except for hooks Replace forwarded ref instances of useProvidedRefOrCreate with useMergedRefs Mar 24, 2026
// 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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we add a TODO:? We sometimes grep for those

Copy link
Copy Markdown
Contributor

@hectahertz hectahertz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good! Let's get the integration tests passing and merge 🚀

@github-actions github-actions bot requested a deployment to storybook-preview-7644 March 31, 2026 14:42 Abandoned
@github-actions github-actions bot temporarily deployed to storybook-preview-7644 March 31, 2026 14:56 Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-7644 April 1, 2026 15:22 Inactive
@francinelucca francinelucca added the Canary Release Apply this label when you want CI to create a canary release of the current PR label Apr 1, 2026
@primer-integration
Copy link
Copy Markdown

👋 Hi from github/github-ui! Your integration PR is ready: https://github.com/github/github-ui/pull/17627

@primer-integration
Copy link
Copy Markdown

Integration test results from github/github-ui:

Running  CI   Running
Passed  VRT   Passed
Passed  Projects   Passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Canary Release Apply this label when you want CI to create a canary release of the current PR integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm skip changeset This change does not need a changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants