Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142353

When do I use attrs vs passing props directly with styled-components?

$
0
0

I am a bit confused as to when I should use the attrs constructor to pass props in styled-components:

const Example = styled.div.attrs(props => ({
  style: {
    color: props.color
  }
}))``;

and when I should just pass them direcly:

const Example = styled.div`
    color: ${props => props.color}
`

I understand that the first method is changing the in-line style and the second is generating a class but I'm unsure of the implications of these differences.

I have seen an error in the console that says "Over 200 classes were generated for component styled.div. Consider using the attrs method, together with a style object for frequently changed styles."

But at the same time it says in the docs "The rule of thumb is to use attrs when you want every instance of a styled component to have that prop, and pass props directly when every instance needs a different one."

Why not just use one of these methods all the time? When should I use each one and why?


Viewing all articles
Browse latest Browse all 142353

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>