Last active
November 22, 2023 20:30
-
-
Save MR-Mostafa/bc593f38eb347e4224c917424bc61109 to your computer and use it in GitHub Desktop.
Build strongly typed polymorphic components with React and TypeScript - Part 4
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 = 'button'>({ | |
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