Best practices for typing a reusable button?
I have a React-TypeScript-Material UI app with a Button component but I'm not sure if what I'm doing is a good practice. Basically in the ButtonProps 4 out of 5 props are optional. Is this ok or should I structure the code in a different way? Perhaps not turning the button into a reusable component at all?
Solution:Jump to solution
You can have something like this
```ts
type ButtonProps = React.ComponentProps<"button"> & {
leftIcon?: ReactElement<SVGSVGElement>;...
3 Replies
I think with components arch, the best part is you decide what and how much connection you want.
IMO, if you have 3 / 4 button types (Default, Outline, Success, Danger, etc) then this code makes sense.
But if you almost have the same button types across your app then this can be avoided.
Solution
You can have something like this
and then
+1 for @Matvey's solution of extending
ComponentProps
.
I'd also like to argue, that generally, have as much of the props optional as possible. If I take component made by someone else, most of the time the first thing I want to do is render it, to see how it behaves "by default" and what happens when I start to add additional props.
In this case it doesn't matter that much, because everyone generally understands what children
do, but I'd still have it optional as a rule of thumb. And of course, this is my personal preference, do it or don't based on how it feels to you.
Another thing I'd change is leftIcon
to ReactNode
, instead of ReactElement<SVGSVGElement>
. That way you can pass something that's not a svg. (For example loading spinner component).