Skip to content

Instantly share code, notes, and snippets.

@satinP
Last active January 7, 2024 20:13
Show Gist options
  • Save satinP/4808077764317f354fa0099b4167c180 to your computer and use it in GitHub Desktop.
Save satinP/4808077764317f354fa0099b4167c180 to your computer and use it in GitHub Desktop.
Polymorphic TypeScript React component with `as` prop
import React from "react";
type ButtonProps<T extends React.ElementType> = {
as?: T
} & React.ComponentProps<T>
export const Button = <T extends React.ElementType = "button">(
{ as: Element = 'button', props }: ButtonProps<T>
) => <Element {...props} />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment