Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't forward component property to styled Mui Button

I've got a styled MUI Button:

const SecondaryButton = styled(Button)<ButtonProps>(({ theme }) => ({
  ...
}));

export default SecondaryButton;

If I use it like this:

<label htmlFor="contained-button-file">
   <input id="contained-button-file" multiple type="file" hidden onChange={this.selectFiles} />
      <SecondaryButton component='span'>
            Dateiauswahl
       </SecondaryButton>
</label>

I get the following typescript error:

Type '{ children: string; component: string; }' is not assignable to type 'IntrinsicAttributes & { children?: ReactNode; classes?: Partial<ButtonClasses> | undefined; color?: "inherit" | "error" | ... 5 more ... | undefined; ... 9 more ...; variant?: "text" | ... 2 more ... | undefined; } & Omit<...> & CommonProps & Omit<...> & MUIStyledCommonProps<...>'.
Property 'component' does not exist on type 'IntrinsicAttributes & { children?: ReactNode; classes?: Partial<ButtonClasses> | undefined; color?: "inherit" | "error" | ... 5 more ... | undefined; ... 9 more ...; variant?: "text" | ... 2 more ... | undefined; } & Omit<...> & CommonProps & Omit<...> & MUIStyledCommonProps<...>'

If I use the Standard MUI Button instead there is no error. As far as I understand my styled Button should just forward the properties. Hence, it should work.

Any help is appreciated.

like image 544
phe Avatar asked Apr 12 '26 19:04

phe


1 Answers

As mentioned in the doc, due to some limitations, we have to bypass this by type casting, at least at the moment of this writing

const SecondaryButton = styled(Button)(({ theme }) => ({
  // ...
})) as typeof Button;
    ^^^^^^^^^^^^^^^^

export default SecondaryButton;

References

Complications with the component prop

like image 148
hgb123 Avatar answered Apr 15 '26 10:04

hgb123



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!