Skip to content

Replace forwarded ref instances of useProvidedRefOrCreate with useMergedRefs#7644

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

Replace forwarded ref instances of useProvidedRefOrCreate with useMergedRefs#7644
iansan5653 wants to merge 34 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: 7b45d61

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.

@iansan5653
Copy link
Copy Markdown
Contributor Author

iansan5653 commented Apr 3, 2026

😞 This is the most frustrating PR ever. I have no idea why the integration CI is failing, and the only way I can try things is by changing files in this PR, then waiting for the canary release, then rerunning the integration CI. So I'm going to have to revert each individual file one by one until I figure out which change is causing the failure. Very painful.

  • ActionList
  • AnchoredOverlay
  • ButtonGroup
  • Checkbox
  • Dialog
  • Tabs
  • FilteredActionList
  • useMergedRefs
  • PageHeader
  • TextInput
  • Tooltip

@github-actions github-actions bot requested a deployment to storybook-preview-7644 April 3, 2026 18:59 Abandoned
@github-actions github-actions bot temporarily deployed to storybook-preview-7644 April 3, 2026 19:09 Inactive
@primer-integration
Copy link
Copy Markdown

⚠️ Integration PR Outdated

This integration PR does not have the latest commit from the primer/react PR.

Integration PR references: 07eeee358aba88eb1baa4c41d0f76f9b38236fce
Latest commit on primer/react PR: a120837acaf65cff30db6a33303a42e287c8d351

Please update the integration PR to reference the latest commit from the primer/react PR before reviewing workflow results.

@primer-integration
Copy link
Copy Markdown

⚠️ Hi from github/github-ui! The integration workflow could not find a canary version for the latest commit on this PR.

A successful canary CI run (i.e., a valid canary version published via the release.yml workflow) must exist for the latest commit before integration checks will succeed.

Note: Make sure the Canary Release label is applied to the PR — the release.yml workflow requires this label to publish a canary version.

Next steps:

  1. Wait for the release.yml canary CI run to complete successfully for the latest commit on this PR.
  2. Once a valid canary version exists, re-trigger the integration workflow by visiting the primer-react-pr-test workflow page, clicking Run workflow, and pasting this PR's URL.

For more details, see this workflow run.

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