Skip to content

Instantly share code, notes, and snippets.

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