r/reactjs • u/swyx • Feb 18 '19
Why I Write CSS in Javascript - Max Stoiber
https://mxstbr.com/thoughts/css-in-js/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
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
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
16
u/ArcanisCz Feb 18 '19
All of mentioned points can be achieved through good discipline (scoped css file per component) or css-modules.