question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

attrs() are no longer merged in v4

See original GitHub issue

Description

👋 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

Steps to reproduce

  1. Create a styled-component with a className provided via the attrs() method
  2. Create another styled-component that wraps the original component styled(InnerComponent) which also provides a className via the attrs() method
  3. 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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
austingreendevcommented, Dec 10, 2018

That makes much sense! Thanks for the diagram at the end of #2258

Thanks for the help. Closing.

1reaction
austingreendevcommented, Dec 5, 2018

I don’t think we should be doing that, as you should manually be opting into letting an attribute being overriden, so I think the behavior in v4 is correct

Agreed! I’m assuming the behavior worked for us since we were using the styled(Component) syntax rather than Component.extend.

Right now, your lower-level component basically has to explicitly allow overriding the attr, which is exactly what attrs is for

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 in attrs() and I’m still unable to get className to be provided.

export const InnerComponent = styled.div.attrs(props => ({
  className: classNames(props.className, "component-inner")
}))``;

export const OuterComponent = styled(InnerComponent).attrs({
  className: "component-outer"
})``;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Important changes (deprecations) coming in Power Apps and ...
On December 4, 2020, the legacy web client will no longer be available. Organizations should make the transition to Unified Interface as soon...
Read more >
Customizing the behavior of cached fields - Apollo GraphQL
Merging non -normalized objects. You can use a merge function to intelligently combine nested objects that are not normalized in your cache, assuming...
Read more >
Manage manifest files - Android Developers
This page describes how manifest merging works and how you can apply merge preferences to resolve merge conflicts.
Read more >
merge attributes - how do I get them to function properly?
The component has an x axis padding of px-4. I pass px-0, but there is no effect. What am I doing wrong? rbd....
Read more >
API Reference - styled-components
Read more about how to adapt styling based on props in the Adapting based on ... v4.3. If you choose to wrap another...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found