简体   繁体   中英

Add type for onClick property which is in …props in React TypeScript

I have a button component in which I'm trying to write the prop types using type and I see this error in the console. Could anyone please help?

Type '{ children: string; label: string; onClick: () => void; }' is not assignable to type 'IntrinsicAttributes & Props'.
  Property 'onClick' does not exist on type 'IntrinsicAttributes & Props'.  TS2322

Excerpt from my code

type Props = {
  label: string;
  children: ReactNode;
};

const Button = ({ label, children, ...props }: Props) => (
  <button label={label} {...props}>
    {children}
  </button>
);

You need to add onClick into Props like:

type Props = {
  label: string;
  children: ReactNode;
  onClick: () => void;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM