You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature Request: Pass on undefined props to final component #4338
importstyledfrom"styled-components";// An empty component that just logs it's propsconstComponent=(props: {prop: unknown})=>{console.log(props);returnnull;};constStyledComponent=styled(Component)``;exportdefaultfunctionPage(){return<StyledComponentprop={undefined}></StyledComponent>;}
Steps to reproduce
Set up reactjs with Styled-Components.
Run the above code
Change StyledComponent in Page to Component
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 pass aria-describedby={undefined} if there's no Dialog.Description, which is impossible without an ugly wrapper.
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.
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: mui/material-ui#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:
constButtonBase=(props)=>{// ...// `role="button"` will not be reset here since styled-components strips away the `role={undefined}`return<ButtonBaseRootrole="button"{...props}/>;}constSwitchBaseRoot=styled(ButtonBase)``;constSwitchBase=()=>{// ...return<SwitchBaseRootrole={undefined}/>;}
Activity
aarondill commentedon Aug 3, 2024
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.styled-components/packages/styled-components/src/models/StyledComponent.ts
Lines 142 to 146 in e0019ba
aarondill commentedon Aug 3, 2024
Since the props passed to the final outermost styled component (
StyledComponentin the example) and.attrs()follow this same code path, this issue affects them both.mtsknn commentedon Sep 21, 2024
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
Linkcomponent (<a>) which defaults torel="noopener noreferrer"; settingrel={undefined}doesn't work anymore.Lukas-Kullmann commentedon Oct 23, 2024
For what it's worth, this also causes this bug in Material UI when using styled-components: mui/material-ui#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:
fix: preserve explicitly passed undefined props
fix: preserve explicitly passed undefined props
fix: preserve explicitly passed undefined props (#5683)
quantizor commentedon Mar 19, 2026
This fix is available in
[email protected]. Please try it and report any issues:6.x maintenance: attrs and undefined props, RSC global styles, types …