attrs() are no longer merged in v4
See original GitHub issueDescription
👋 Hello. I’ve been investigating a migration from v3
to v4
and have been seeing some different results with the .attrs()
method while extending other styled-components
.
Within our component library we have several styled components that use CSS classes applied via the attrs()
method. Example
We also have components that “wrap” (not using the .extend()
API) to apply additional classNames.
const Tooltip = styled.div.attrs({ className: 'tooltip' })``;
const LightTooltip = styled(Tooltip).attrs({ className: 'tooltip--light' })``;
In v4
it doesn’t seem that this type of attrs()
merging is possible. Doesn’t seem to be possible even with a .attrs(props => ({}))
callback. Is this expected?
It seems some attributes are shallow merged (like styles
in #1877). Should this have the same treatment if it’s a common pattern?
Environment
CodeSandbox default create-react-app
environment.
Reproduction
styled-components@3.4.10
withattr
mergingstyled-components@4.1.2
withoutattr
merging
Steps to reproduce
- Create a styled-component with a
className
provided via theattrs()
method - Create another styled-component that wraps the original component
styled(InnerComponent)
which also provides aclassName
via theattrs()
method - Only the outer className is being provided.
Expected Behavior
Similar to v3
of styled-components, the attrs are merged if a component is extended.
Actual Behavior
Only the outer attrs()
methods are respected.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:5 (1 by maintainers)
Top GitHub Comments
That makes much sense! Thanks for the diagram at the end of #2258
Thanks for the help. Closing.
Agreed! I’m assuming the behavior worked for us since we were using the
styled(Component)
syntax rather thanComponent.extend
.Is there a “best” approach to reliably allow
className
to be overridden in the lower component? I’ve updated the v4 Codesandbox to include another example with the new props callback inattrs()
and I’m still unable to getclassName
to be provided.