-
-
Notifications
You must be signed in to change notification settings - Fork 347
fix(ui): forward refs in Button so Radix asChild slots work #929
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
Merged
+20
−16
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🧩 Analysis chain
🌐 Web query:
In React 19 + TypeScript, what is the recommended way to type a polymorphic component with anasprop and forwarded refs without usingas unknown ascasts?💡 Result:
In React 19, the
refprop is now treated as a standard prop, which simplifies the implementation of polymorphic components by removing the need forforwardRef[1][2][3]. To create a type-safe polymorphic component without usingas unknown ascasts, you should define a generic component that accepts anasprop and destructuresrefdirectly from the props object [2][3]. The recommended approach involves: 1. Defining a generic type for your component props that extendsReact.ElementType[4][5]. 2. UsingReact.ComponentPropsWithRef<C>to ensure the component correctly inherits the props and ref type of the element specified by theasprop [6][4]. 3. Destructuringrefandasfrom the props and passing them directly to the underlying element [2]. Example Pattern: type PolymorphicProps<C extends React.ElementType, Props = {}> = Props & { as?: C } & Omit<React.ComponentPropsWithRef, keyof Props | 'as'>; export const PolymorphicComponent = ({ as, ref,...props }: PolymorphicProps<C, { children?: React.ReactNode }>) => { const Component = as || 'div'; return <Component ref={ref} {...props} />; }; By destructuringrefas a standard prop, you avoid the complex type signatures previously required byforwardRef[2][3]. This pattern allows TypeScript to correctly infer the element type and its associated ref, maintaining full type safety without requiring type assertions [4]. If you are working in a codebase that still requiresforwardReffor compatibility, you can continue to use it, but it is no longer necessary for new components in React 19 [1][2].Citations:
🏁 Script executed:
Repository: TanStack/tanstack.com
Length of output: 166
🏁 Script executed:
Repository: TanStack/tanstack.com
Length of output: 1636
🏁 Script executed:
Repository: TanStack/tanstack.com
Length of output: 493
🏁 Script executed:
Repository: TanStack/tanstack.com
Length of output: 764
🏁 Script executed:
Repository: TanStack/tanstack.com
Length of output: 258
🏁 Script executed:
Repository: TanStack/tanstack.com
Length of output: 366
🏁 Script executed:
Repository: TanStack/tanstack.com
Length of output: 79
🏁 Script executed:
Repository: TanStack/tanstack.com
Length of output: 630
🏁 Script executed:
Repository: TanStack/tanstack.com
Length of output: 93
Update to use React 19's simpler polymorphic pattern without
forwardRef.The component has a polymorphic type contract (
ButtonComponentis generic overTElement) but the implementation uses monomorphicforwardRef<HTMLElement, ButtonInnerProps>, creating a type mismatch that requires unsafe casts (line 117:props as ..., line 145:as unknown as ButtonComponent). Since the codebase is on React 19.2.3,refcan be treated as a standard prop, eliminating the need forforwardRefentirely and removing the type coupling issue.♻️ Suggested direction
Also applies to: 117-117, 145-145
🤖 Prompt for AI Agents