r/reactjs • • Feb 18 '19

Why I Write CSS in Javascript - Max Stoiber

https://mxstbr.com/thoughts/css-in-js/
15 Upvotes

20 comments sorted by

16

u/ArcanisCz Feb 18 '19

All of mentioned points can be achieved through good discipline (scoped css file per component) or css-modules.

3

u/BonafideKarmabitch Feb 18 '19 edited Feb 18 '19

indeed there are many ways to do the same thing. but there are also other minor benefits of cssinjs that arent covered by css modules, and also note that part of his whole pit of success argument is that discipline doesnt last/scale

4

u/Suepahfly Feb 18 '19

I've used css-modules quite extensively but haven't given styled-components a real try yet. What do you think are some of the benefits of styled-components over css-modules?

3

u/swyx Feb 18 '19

(minor one first) how well does css modules do nesting? (im not 100% sure but i think they only do css selector chaining) styled components lets you write SASS style nesting

for me the bigger one is being able to use js to dynamically pass in any part of the css, including doing inline logic (beyond what calc can do), or adding blocks of css based on some state, including at the media query level. very powerful and much more expressive (and convenient) than css variables.

if you’re not using the dynamic stuff, then the major cssinjs libraries all have ways to extract your css out to static files which brings you back to feature parity with css modules.

these are nuanced-er things that max didnt even get to in his post since his post wasnt intended to contrast with css modules.

4

u/04fuxake Feb 19 '19

You can make CSS modules using SASS easily. The interesting thing I’ve found is that with a significantly small component, nesting becomes less of a requirement because the CSS is scoped so tightly.

1

u/swyx Feb 19 '19

yea obviously i shouldnt have put it first, it wasnt even a good point 🤦‍♂️

3

u/programmer-bill Feb 18 '19

CSS Modules is just CSS, so it's trivial to use it with Sass or Less. Create-React-App even has built-in support for it.

2

u/swyx Feb 18 '19

ah yes i forgot.

2

u/Suepahfly Feb 19 '19

Thanks for the explanation!

1

u/ArcanisCz Feb 18 '19

Well, theese (good) points wer not present (aside from dynamic styling mention) in article, why?

10

u/bentr0k Feb 18 '19

Yes! Deleting styling has become so much more relaxed since using `css-in-js`. I don't even think about it anymore :)

3

u/stephenkiers Feb 19 '19

I was anti css-in-is until I tried styled components. I still include the grid in css, but everything else is styled-components. My favorite part is the ability to add props to styled components for dynamic styles.

5

u/clinisbut Feb 18 '19

I still don't get whole CSS in JS thing...

2

u/zephyrtr Feb 18 '19

What happens with global styles? Or do I have to apply the same margins etc to inputs individually? If one component calls another component, and both are styled -- who gets precedence? I come away with a lot of questions, personally. I imagine the only way to know if you'd like using this library is by using it.

6

u/nikola1970 Feb 18 '19

You can set global styles with styled components. About who gets precendence - you do not think that way. Every component (button, paragraph, div) is a component of its own which have scoped css inside it so you never get css clashing. Try styled-components and you will see. I was not a fan at first, now its my default styling option. I forgot wht scss and plain css are...

9

u/C0git0 Feb 18 '19

CSS in JS is just absurd. Requires more code to write, more cpu to run, and more payload over the wire. All of the supposed plus sides can be accomplished in a more performant way with open standards.

4

u/darrenturn90 Feb 18 '19

You could argue the same for html in js - however it has its strengths and advantages - and css even with its fancy new variables still isn’t a dynamic and conditional language without JavaScript doing the heavy lifting behind the scenes - one only needs to look to css Houdini to see that the line between css and JavaScript in the browser is already being blurred.

0

u/friendshrimp Feb 18 '19

Thank you agreed

1

u/rodrigocfd Feb 18 '19

IMO the greatest benefit of writing CSS in CSS (lol) is hot-reloading.

5

u/spryes Feb 18 '19

styled-components hot-reloads for me with regular tag styles. Changing CSS in `createGlobalStyle` doesn't seem to though