Last active
November 22, 2023 20:30
-
-
Save MR-Mostafa/f31ec08813dafd4fa6650dedfe7c2a89 to your computer and use it in GitHub Desktop.
Build strongly typed polymorphic components with React and TypeScript - Part 3 (Support Other Valid Props, Based On The Value Of As)
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
import * as React from 'react'; | |
type ButtonProps<C extends React.ElementType> = { | |
as?: C; | |
children: React.ReactNode; | |
} & React.ComponentPropsWithoutRef<C>; // 👈 look here | |
export const Button = <C extends React.ElementType>({ | |
as, | |
children, | |
...otherProps | |
}: ButtonProps<C>) => { | |
const Component = as || 'button'; | |
return <Component {...otherProps}>{children}</Component>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment