-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[docs] component
property on MUI components wrapped in styled
in v6
#44931
Comments
Hey @khanilov, thanks for reaching out.
This is a known issue, and I'm not sure why it worked for you before. The issue is reported here: #29875. Here's the workaround: #29875 (comment)
As you can see in the answer above, The meaning of that section is that you can provide any const StyledDiv = styled('div')``;
const Demo = () => {
return <StyledDiv component={'a'} />;
}; You would do const StyledA = styled('a')``;
const Demo = () => {
return <StyledA />;
}; And if you need the component prop, you would do const StyledDiv = styled('div')<{ component: React.ElementType }>``;
const Demo = () => {
return <StyledDiv component={'a'} />;
};
Taking your example: import { Box, styled } from '@lingoda/ui';
const Demo = () => {
return <StyledBox component={true ? 'button' : undefined} a={true} b={false} c={true} />;
};
const StyledBox = styled(Box)<{ a: boolean; b: boolean; c: boolean }>(({ a, b, c }) => ({
color: a ? 'blue' : 'black',
background: b ? 'white' : 'yellow',
borderRadius: c ? 1 : 2,
})); You can transform it to import { styled } from '@lingoda/ui';
const Demo = () => {
return <StyledDiv component={true ? 'button' : undefined} a={true} b={false} c={true} />;
};
const StyledDiv = styled('div')<{ a: boolean; b: boolean; c: boolean, component: React.ElementType | undefined }>(({ a, b, c }) => ({
color: a ? 'blue' : 'black',
background: b ? 'white' : 'yellow',
borderRadius: c ? 1 : 2,
})); The takeaway is: If you're using I hope this helps. Let me know. |
Thank you, @DiegoAndai, for response and code suggestions.
Indeed that's major oversight on my side. We have some wrapping that bring this Given I need
We use |
It's easier to explain the other way around: There's no benefit of using |
Got you. Thanks for responses! |
This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue. Note @khanilov How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey. |
Related page
https://mui.com/material-ui/migration/upgrade-to-v6/#breaking-changes-affecting-types
Kind of issue
Unclear explanations
Issue description
Hello, recently we tried to migrate our project to material v6, but faced an issue with
styled
andcomponent
property, that blocks us from completing migration. I wasn't able to find clear answers on that in docs, so I decided to open this issue to clear up the air around that.First, seems like there is no more
component
property available in return type ofstyled()
on any mui component in v6 compared to v5, not only theBox
component that mentioned in breaking changes. Probably this should be mentioned properly in migration docs.Second, the section about
Box
itself is confusing.It suggests two options to fix missing
component
property in return type ofstyled(Box)
.First suggestion is:
But next code have same typescript error of missing
component
property, as with using aBox
component:So it's unclear what this first suggestion is about.
Second suggestion is:
This works, but only when you don't have additional custom properties passed to
styled
. Once you have them, typescript throws an error.We have a lot of code in our code base that rely both on
styled
with additional custom properties and usage ofcomponent
property on returned component and I am struggling to find clear documentation on that manner.component
property in returning type ofstyled
gone?styled('div')
havecomponent
property in returning type or what was the meaning of that suggestion in migration guide?styled
to retaincomponent
property isn't an option due to additional custom properties passed tostyled
, what else we can do?Thank you for your hard work, and looking forward for replies.
Context
In v5 we could write code like this to leverage both styles customisation and component composition:
How can we achieve the same result in v6?
Search keywords: component, property, styled, typescript
The text was updated successfully, but these errors were encountered: