[英]Button Pass Onclick Event
我有一个具有onClick属性的button.tsx。
import * as React from 'react';
interface Props {
className?: string;
text: string;
onClick?(event: React.MouseEvent<HTMLButtonElement>): void;
type?: string;
}
const submitButton = (event: React.MouseEvent<HTMLButtonElement> , props:
Props) => {
event.preventDefault();
props.onClick(event);
};
export const ButtonComponent = (props: Props) => {
return (
<button
className={props.className}
type={props.type}
onClick={submitButton(event, props)}
>
{props.text}
</button>
);
};
如何将事件传递给SubmitButton函数?
现在我有这个错误:
“事件”类型的参数不能分配给“鼠标事件”类型的参数。 类型“事件”中缺少属性“ altKey”。
您在渲染过程中立即调用了submitButton
,而不是将函数作为处理函数传递,因此事件不存在,并且您不传递任何内容( submitButton
的返回类型为onClick)
作为函数传递给onClick
<button className={props.className}
type={props.type}
onClick={event => submitButton(event, props)}>
{props.text}
</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.