我设计了下面的按钮使用TailwindCSS与onClick事件.
<button className="border bg-purple-600 rounded-lg text-white px-2 py-1 mb-4" onClick={handleClick}>
Add Item
</button>
现在,我try 为该按钮创建一个Reaction组件.我想在任何地方使用这个组件,有时它会有onClickprops ,有时onMouseDown等等.所以我试图在组件内的按钮元素上设置传递给我的组件的任何props .我想出了以下几点.
function Button({
children,
...props
}: {
children: React.ReactNode;
props?: any;
}) {
return (
<button
className="border bg-purple-600 rounded-lg text-white px-2 py-1 mb-4"
{...props}
>
{children}
</button>
);
}
我不知道要设置什么类型的props ,所以我暂时将其设置为any
.此代码不显示任何错误.
但当我try 使用下面的组件时,它显示: Type '{ children: string; onClick: () => void; }' is not assignable to type 'IntrinsicAttributes & { children: ReactNode; props?: any; }'
<Button onClick={handleClick}>
Add Item
</Button>
我怎么才能让它正常工作呢?