Skip to content

Deprecate useProvidedRefOrCreate and migrate everything except for hooks#7644

Draft
iansan5653 wants to merge 5 commits intocreate-use-combined-refsfrom
replace-use-provided-ref-or-create
Draft

Deprecate useProvidedRefOrCreate and migrate everything except for hooks#7644
iansan5653 wants to merge 5 commits intocreate-use-combined-refsfrom
replace-use-provided-ref-or-create

Conversation

@iansan5653
Copy link
Contributor

@iansan5653 iansan5653 commented Mar 9, 2026

In #7638 I created a useCombinedRefs 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: While I deprecated the useProvidedRefOrCreate hook so that we can prepare to remove it from the public API in a future release, I didn't migrate every case and I think we'll probably want to keep it around in an undeprecated internal API. The cases where it still applies are very specific to utility hooks like useAnchoredPosition. These are expected to accept and return ref objects and would require significant breaking changes to make them compatible with useCombinedRefs since they'd have to return callback 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

changeset-bot bot commented Mar 9, 2026

⚠️ No Changeset found

Latest commit: 422c4e4

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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant