Reactjs 为html组件键入onClick函数
每次我尝试在我的组件中键入类似于Reactjs 为html组件键入onClick函数,reactjs,typescript,Reactjs,Typescript,每次我尝试在我的组件中键入类似于onClick的内容时,如下所示: export interface IClearButton extends SpaceProps { onClick: Function; } export const ClearButton: React.FC<IClearButton> = ({onClick}) => { return ( <button onClick={onClick}> <SomeCo
onClick
的内容时,如下所示:
export interface IClearButton extends SpaceProps {
onClick: Function;
}
export const ClearButton: React.FC<IClearButton> = ({onClick}) => {
return (
<button onClick={onClick}>
<SomeComponent />
</button>
);
};
Type '{ onClick: Function; }' is not assignable to type 'IntrinsicAttributes & IClearButton & { children?: ReactNode; }'.
Property 'onClick' does not exist on type 'IntrinsicAttributes & IClearButton & { children?: ReactNode; }'
目前,我只是说
onClick:any
,这不太理想。在我的类型定义中,如何在单击类型时扩展内在的,或者使用函数强制覆盖它?您应该使用接口详细的HTMLPROPS
和InputtmLattributes
import React, { DetailedHTMLProps, InputHTMLAttributes } from 'react';
interface MyProps extends DetailedHTMLProps<
InputHTMLAttributes<HTMLButtonElement>,
HTMLButtonElement
> {
name: string;
}
import React,{DetailedHTMLProps,inputtmLattributes}来自'React';
接口MyProps扩展了DetailedHTMLProps<
InPuttmLattributes,
HTMLButtoneElement
> {
名称:字符串;
}
在这种情况下,onClick
的类型将是var onClick:(事件:React.MouseEvent)=>void
为您创建。我不会收到与您相同的错误: