簡體   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