reactjs - "TypeError: Cannot read properties of undefined (reading 'withConfig')" styled-components/react/rollup/babel - Stack Overflow
styled-components withConfig not supported
styled.css.withConfig is not a function Error using Rollup
withConfig not supported
Videos
I was so happy in my previous repo with styled-components 5.3.6, making thousands of styled-components components with all that dynamic stuff with the help of custom props. I made sure to respect the reserved prop names like 'disabled' (I would call it isDisabled instead) and it was awesome.
Now, new project, new repo, I decide to take the latest packages, including styled-components 6.1.2 and I would adapt to new changes. For once I even managed to avoid the usual. So I work for many hours without noticing any red warning in the console related to my custom props... until today:
Warning: React does not recognize the `isPanelExpanded` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `ispanelexpanded` instead. If you accidentally passed it from a parent component, remove it from the DOM element.
Of course it doesn't recognize it. That prop was never meant for the DOM elements. And I suppose this is why:
Dropped automatic prop filtering; use transient props ($ prefix) for stuff you don't want to be passed to child component / HTML (styled-components/releases/tag/v6.0.0)
The workarounds:
-
$isPanelExpanded. Ugly.
-
styled('div').withConfig({ shouldForwardProp: (prop) => !['isPanelExpanded'].includes(prop)}). No. Just.. no.
-
Roll back to an outdated styled-components like a piece of sh**?
Someone please tell me I missed the simple elegant solution to this nonsense. Wake me from this nightmare, please. *screams (from home, not at the office)*
Use Transient props
TL;DR just prefix your attriibute with
$sign. example$borderColor,$black,$any,$attribute.
If you want to prevent props meant to be consumed by styled components from being passed to the underlying React node or rendered to the DOM element, you can prefix the prop name with a dollar sign ($), turning it into a transient prop.
// typescript example
const BaseButton = styled(Button)<{ $borderColor: string }>`
border-color: ${({ $borderColor }): string => $borderColor};
`;
// js
const BaseButton = styled(Button)`
border-color: ${({$borderColor}) => $borderColor}
`;
// usage
<BaseButton $borderColor="red">Button</BaseButton>
2nd method
Checkout shouldForwardProp
const Comp = styled('div').withConfig({
shouldForwardProp: (prop, defaultValidatorFn) =>
!['hidden'].includes(prop)
&& defaultValidatorFn(prop),
}).attrs({ className: 'foo' })`
color: red;
&.foo {
text-decoration: underline;
}
`;
render(
<Comp hidden draggable="true">
Drag Me!
</Comp>
);
Your existing code was already right, but react gave you two options :
1) use lower case than snake-case
2) remove the attribute from DOM (you took this approach)
From the code I can see that you need the prop borderColor, but in the custom styling, you separated the props
({borderColor,... rest}) => <Button {...rest} />
You removed the border Color prop but you try to access in styled props the next line.
Instead try to rename the prop to bordercolor if you want warning to go away or just ignore warning.