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.

Emotion generating n different classes not having a common class based on props

See original GitHub issue

Hi I’m rendering a list of items with background color as props.

const Thumbnail = styled.div<{ url: string }>`
  background-color: url('${props => props.color}');
  border-radius: 2px;
  height: 104px;
  margin-bottom: 8px;
  width: calc(33.33% - 5px);
`;

used as

<Thumbnail url={img.thumbnailLink} key={index} />

Emotion generates entirely different classes for all the Thumbnail components, is it the expected behaviour, what I was expecting was emotion generating 2 classes here One for the common styles

  border-radius: 2px;
  height: 104px;
  margin-bottom: 8px;
  width: calc(33.33% - 5px)

and one based on prop value

background-color: red

Any help will be appreciated

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
Andaristcommented, Apr 16, 2020

Yes, both classes would have common properties but it doesn’t mean that contents of both would appear anywhere in the code unless you are talking about SSRed output. I don’t see this really as much of an issue - compression should deal with those nicely and usually when SSRing you don’t necessarily produce tons of different rules, sure - there are some, but only what is actually used by the current SSRed screen, nothing more than that.

1reaction
Andaristcommented, Mar 20, 2020

It is expected and it lets us overcome “insertion order problem” which is described here: https://emotion.sh/docs/composition

Read more comments on GitHub >

github_iconTop Results From Across the Web

Emotion 9 to 10 - how to pass external classes properly #1196
I think you still have the straightforward case of styling an HTML button with multiple style definitions and can pass an array to...
Read more >
Class Names - Emotion
It can be useful to create a className that is not passed to a component, for example if a component accepts a wrapperClassName...
Read more >
Emotion CSS-in-JS - how to add conditional CSS based on ...
With regards to creating a new styled component on every render, yes it still holds true. Just as you would not want to...
Read more >
Global Styling with Material-UI Theme Overrides and Props
One of the easiest and most common approaches to customizing Material-UI components is using props and classes for one-time use.
Read more >
Class Components With Props - WebStorm Guide - JetBrains
When they have state (or when they need access to React's lifecycle methods), we use a class-based component instead. In recent years, React...
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