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.

Managing highly dynamic styles

See original GitHub issue

Example component:

const Comp = styled.div`
     /*lots of styles here*/
     animation-delay: ${ props => props.index * 0.1 };
`;

With this component rendered over a collection of items, we’ll generate a new style tag with an associated class for each one.

Is there a recommended way of working with these kinds of styles so as not to thrash the head with a new style tag everytime render is called on Comp?

  • One solution could be to move the /* lots of styles here */ into another component that doesn’t include the dynamic properties that cause huge amounts of CSS to be rendered into the head. This way the generated style tag only contains the animation-delay property. Still a lot of style tags being injected into the head though and since the injection only happens at render time, this could cause some trashing.
  • Another option is to put the animationDelay into the style prop of the component as we compose the components and not let styled-components handle this.

How would you do this? Is there a recommended approach?

I saw there might be some plans to extract static styles which, as a solution, is similar to our first option above.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:13
  • Comments:42 (27 by maintainers)

github_iconTop GitHub Comments

53reactions
mxstbrcommented, Jul 1, 2017

I’ll finally close this discussion since a lot of things have changed since the initial kickoff by @JamieDixon. (thanks!)

  1. We’ve added a warning when you change styles too often that logs to the console and suggests using inline styles for highly dynamic styles to avoid this kind of confusion.
  2. We now have the .attrs selector, making dynamic styles that come in via props possible with inline styles. You could change the bg prop of this component a lot of times without running into performance issues. (I’d be interested to see the color picker from above reworked this way though!)
const HighlyDynamicComponent = styled.div.attrs({
  style: props => ({
    background: props.bg,
  })
})`
  color: blue;
`
  1. More future-wise we’re focusing on performance at the moment, @geelen has found a way to batch style injection that’ll come out with the next patch release which will speed up initial and rerenders by orders of magnitude. We’ll finally be in a similar spot to all other CSS in JS libraries, which means many more dynamic use cases will now be possible too!

That should clear up most, if not all concerns around highly dynamic styles and styled-components. If you have further issues, suggestions or comments please feel free to open a new issue with your ideas!

46reactions
JamieDixoncommented, Sep 10, 2017

@mxstbr Better late than never!

It works beautifully. Thanks for the great work everyone. SC has made such a difference to our project.

sep-10-2017 11-52-05

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic management: Better decisions in uncertain times
My sense, though, is that achieving truly dynamic management will prove elusive for most organizations until they can figure out how to get ......
Read more >
High Performance Dynamic Styles - Medium
Inline Styles have a number of limitations by design: 1. You can't share an inline style declaration between multiple elements. 2. You need...
Read more >
How To Achieve Dynamic Leadership In 'The New Next' - Forbes
Dynamic leaders understand that we no longer live in a static world. They chose to focus on only one thing at a time....
Read more >
The Dynamics of 8 Different Styles of Leadership | ATD
Insights Into 8 Leadership Styles · Pacesetting, Autocratic Style · Democratic, Participative Style · People-Focused, Affiliative Style.
Read more >
9 types of management styles and when to use them - Asana
Best for: Managers that are leading a more inexperienced team. Under this dynamic, the manager should be the sole decision-maker based on their ......
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