I designed the below button using TailwindCSS with onClick event.
<button className="border bg-purple-600 rounded-lg text-white px-2 py-1 mb-4" onClick={handleClick}>
Add Item
</button>
Now I tried creating a React component for this button. I want to use this component everywhere and sometimes it will have onClick prop, sometimes onMouseDown, etc. So I am trying to set whatever props are passed to my component on the button element inside the component. I came up with below.
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>
);
}
I did not know what to set the type of props so I've set it to any for now. This code does not show any errors.
But when I try to use the component as below, it says: Type '{ children: string; onClick: () => void; }' is not assignable to type 'IntrinsicAttributes & { children: ReactNode; props?: any; }'
<Button onClick={handleClick}>
Add Item
</Button>
How can I get this working?
Your component does't accept a
propsproperty. Instead, you're spreading the rest of the props. This means the above type definition won't match what you're passing to it.Given you're just creating a button wrapper, you can use its default attributes (which already includes
children).You might also want to prevent consumers passing in a
classNamewhich would overwrite your own classes.You could either just add yours after...
or expressly forbid it in the prop type