Open aarondill opened 3 months ago
It's worth noting that this was an intentional decision (see the code in src/models/StyledComponent.ts
), however, allowing undefined to be passed would have the same effect as not passing it, except when using "key" in obj
.
https://github.com/styled-components/styled-components/blob/e0019ba666fab4b5aaa2bff71ba6ad0005a299fd/packages/styled-components/src/models/StyledComponent.ts#L142-L146
Since the props passed to the final outermost styled component (StyledComponent
in the example) and .attrs()
follow this same code path, this issue affects them both.
This worked in v5, or at least I encountered this problem after migrating from v5 to v6. This breaking change isn't mentioned in the v5 to v6 migration guide.
My use case: I have a Link
component (<a>
) which defaults to rel="noopener noreferrer"
; setting rel={undefined}
doesn't work anymore.
For what it's worth, this also causes this bug in Material UI when using styled-components: https://github.com/mui/material-ui/issues/44185
The problem is that you now cannot pass on a prop as explicitly undefined to reset defaults further down. In case of Material UI, it looks something like this:
const ButtonBase = (props) => {
// ...
// `role="button"` will not be reset here since styled-components strips away the `role={undefined}`
return <ButtonBaseRoot role="button" {...props} />;
}
const SwitchBaseRoot = styled(ButtonBase)``;
const SwitchBase = () => {
// ...
return <SwitchBaseRoot role={undefined} />;
}
Environment
Reproduction
Steps to reproduce
StyledComponent
inPage
toComponent
Expected Behavior
In both cases, Component should log
{ prop: undefined, className: "..." }
Actual Behavior
Component only receives the undefined prop when not wrapped in
styled
.Real World Implications
This is an issue when using @radix-ui/react-dialog, since the
Dialog.Content
expects you to passaria-describedby={undefined}
if there's noDialog.Description
, which is impossible without an ugly wrapper.