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.

Identical styled generating different classnames

See original GitHub issue

Version

2.2.1

Hi! I’m writing a HOC for wrapping and enhancing my styled component. I know this kind of pattern has been frowned upon in previous issues (#316), and without going into the details of what my HOC is doing, just trust me when I say I have a valid use case.

To enhance my wrapped component I’m parsing it’s props and generate some styles which I then pass to the styled method of styled-components, passing in my WrappedComponent to be used as a base. My only problem with this approach is that even though I generate the exact same styles a unique class name is created for every component. I’m concerned that this might turn into a performance hog in the long run and would like to know if there’s a better way to “inject” styles into an existing component.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kittencommented, Nov 7, 2017

My only problem with this approach is that even though I generate the exact same styles a unique class name is created for every component.

That sounds either like you’re confusing the static and dynamic class we create, or it sounds like you’re recreating the styled component on every render.

I’m writing a HOC for wrapping and enhancing my styled component. I know this kind of pattern has been frowned upon in previous issues (#316), and without going into the details of what my HOC is doing, just trust me when I say I have a valid use case.

It’s not encouraged for a reason, and I don’t think there is any valid use case to be honest. Sorry, if that comes across as harsh, but HOCs for styling do not scale, and we do not want to recommend them, or be associated with the pattern 😄

0reactions
johnnyBiracommented, Nov 9, 2017

I totally see your arguments agains doing this, personally I think the pros overweight the cons in this particular case.

Another reason for this approach in my option is portabilty. Imagine you’ve defined media querie with their responsive styles, encapsulated inside each styled component. Now you want to reuse the component in another project with different breakpoints. This logic could of course be tailor for every individual component, but I still prefer not having to repeat that logic and also have a common API for responsive props.

Thank you @philpl you’ve been very helpful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

css - Why are my Styled Components' generated class names ...
However, in my current project, class names generated by Styled Components with identical names are colliding.
Read more >
Micro-frontends: Fixing duplicate class name in Styled ...
If multiple micro frontend use styled-components, each instance of styled-components could generate the same class name hash.
Read more >
How to Control Class Names in Styled Components
The class name is generated by MurmurHash algorithm. It generates a unique identifier based on the component id and the evaluated style ......
Read more >
Advanced Usage - styled-components
styled -components generates an actual stylesheet with classes, and attaches those classes to the DOM nodes of styled components via the className prop....
Read more >
Demystifying styled-components - Josh W Comeau
When we render PinkButton , the Tag variable is equal to our Button component. PinkButton will generate a unique class name ( def456...
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