简体   繁体   English

<a>使用 onClick 和 TypeScript</a>响应功能<a>组件</a>

[英]React functional <a> component with onClick and TypeScript

I'm writing a react component in TypeScript:我正在用 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;

But I get the following error:但我收到以下错误:

The 'this' context of type 'void' is not assignable to method's 'this' of type 'Window'.ts(2684)

I'm not sure about what I'm doing wrong, any help will be welcome!我不确定我做错了什么,欢迎任何帮助!

Don't call onClick() , just pass reference as prop不要调用onClick() ,只需将引用作为道具传递

 const Input: React.FC<Props> = ({ children, href, onClick }) => ( <a className="A" href={href} onClick={onClick}> {children} </a> );

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

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