Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is React.ComponentPropsWithoutRef for react typescript

For using React with typescript, what is React.ComponentPropsWithoutRef used for. Is there any documentation for this property?

like image 541
user1388835 Avatar asked Nov 21 '25 07:11

user1388835


1 Answers

The ComponentPropsWithoutRef type can be used to grab all the native attributes of an HTML element as the props type of your component.

You can simply create a type that has all the native button attributes as props like this:

type ButtonProps = React.ComponentPropsWithoutRef<"button">
const Button = ({ children, onClick, type }: ButtonProps) => {
  return (
    <button onClick={onClick} type={type}>
      {children}
    </button>
  )
}

Here a link that talks about usage of ComponentPropsWithoutRef in typescript more. Refer "How to type (extend) HTML elements" and "ComponentPropsWithoutRef vs [Element]HTMLAttributes" sections

like image 86
ketan shinde Avatar answered Nov 23 '25 22:11

ketan shinde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!