繁体   English   中英

为 onClick 属性添加类型,该属性位于 React TypeScript 中的 ...props 中

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

我有一个按钮组件,我正在尝试使用type编写道具类型,并且在控制台中看到此错误。 有人可以帮忙吗?

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

摘自我的代码

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

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

您需要将onClick添加到Props中,例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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