我正在用TypeScript编写一个react组件:
interface Props {
children: React.ReactNode;
href: string;
onClick?: (e: any) => void;
}
const Input: React.FC<Props> = ({ children, href, onClick }) => (
<a className="A" href={href} onClick={onclick(e)}>
{children}
</a>
);
export default Input;
但是出现以下错误:
The 'this' context of type 'void' is not assignable to method's 'this' of type 'Window'.ts(2684)
我不确定自己在做什么错,欢迎任何帮助!
答案 0 :(得分:2)
不要调用onClick()
,只需将引用作为prop传递
const Input: React.FC<Props> = ({ children, href, onClick }) => (
<a className="A" href={href} onClick={onClick}>
{children}
</a>
);