What is the cleanest way to add props to extended components? (typescript) #1935
-
Hi there! I am trying to add additional props to components that I am extending. Here is an example where I am providing some default tailwind styles to I've figured out a way to do it, but it doesn't feel very clean. I have a feeling there's a better way to go about this. If you think you have something better, I'd love to hear it. Thanks!
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hey @williamlmao, You don't really need that Inner component, it doesn't achieve anything. type RootElement = React.ElementRef<typeof AvatarPrimitive.Root>;
type RootProps = React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root> & {
ignoreDefaultStyles?: boolean;
};
const Root = React.forwardRef<RootElement, RootProps>((props, forwardedRef) => {
const { className, ignoreDefaultStyles, ...rest } = props;
return (
<AvatarPrimitive.Root
{...rest}
ref={forwardedRef}
className={cn(
{
'relative flex h-10 w-10 shrink-0 overflow-hidden rounded-full':
!ignoreDefaultStyles,
},
className
)}
/>
);
});
Root.displayName = AvatarPrimitive.Root.displayName; |
Beta Was this translation helpful? Give feedback.
Hey @williamlmao,
You don't really need that Inner component, it doesn't achieve anything.
You could something like this instead: