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.

Best practice for styles with dynamic properties

See original GitHub issue

When styled components use dynamic properties it creates a complete copy of all the rules for each possible value at run time which has potential to bloat SSR payload and in general add more content to memory.

const A = styled.div`
  ... many rules ...

  background-color: ${props => props.bg};
`

In this example many rules is duplicated whenever props.bg is changed.

In general I have been using the styled hoc to wrap a base styled component of static rules with my dynamic rules like:

const A = styled.div`
   ... many rules ...
`

const B = styled(A)`
  background-color: ${props => props.bg};
`

The many rules are created once and the dynamic rules classes only contain what change.

Could styled achieve this abstraction automatically by detecting dynamic parts? Could we “mark” with custom syntax which rules need to be added to dynamic classes within one component?

Im interested in hearing how other people handle this problem or if it is currently not a concern.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
k15acommented, Jun 22, 2017

For highly dynamic styles you can also use the .attrs method:

const ColorAnimation = styled.div.attrs({
  style: props => ({
    color: props.color
  })
})`
  // static styles
`
1reaction
k15acommented, Jun 22, 2017

This will probably come with version 3 of styled-components. We try to extract every static style property and only inject dynamic ones at run times.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using dynamic styling information - Web APIs | MDN
In many cases, and where possible, it is best practice to dynamically manipulate classes via the className property since the ultimate ...
Read more >
Dynamic style - manipulating CSS with JavaScript - W3C Wiki
In this article we will look at how to dynamically update the styling applied to your elements by manipulating your CSS at runtime...
Read more >
How to Create CSS Custom Properties That Dynamically ...
Read How to Create CSS Custom Properties That Dynamically Update with React & JavaScript at Space Jelly.
Read more >
A Strategy Guide To CSS Custom Properties
Custom properties work differently. Where custom properties are concerned, dynamically scoped means they are subject to inheritance and the ...
Read more >
Making Styles Repeatable: Dynamic Styling in React using ...
DRY and Maintainable: Instead of declaring the same styles again and again each time you need them, dynamic styles are declared once and ......
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