by kowdermeister · 9y ago · 0 comments
Just look at https://github.com/rtsao/styletron there's a whole "how to use it" page, but zero info WHY to use it. Not even a section about valid use cases.
IMHO SASS covers almost all cases of CSS "at scale".
> It seems like mixing the actual styles in with the JS violates separation of concerns and clutters up the actual logic.
It does not just seem like it... it does violate it for no good reason.
The implementation of a UI widget is ultimately a combination of CSS, HTML, and JS which are coupled to some degree, so by colocating them together, the component is easier to manage. But to do this effectively, you need some way to get around the global nature of CSS. CSS modules and CSS-in-JS are means of achieving this.
A good blog post about this topic is: https://medium.com/seek-developers/the-end-of-global-css-90d...
But you can still keep CSS in a CSS file inside a module folder. SASS or any other CSS build took could grab it from there.
> get around the global nature of CSS
That can be done with proper scoping supported naturally by CSS.
Local CSS is still not as bad as moving CSS declarations to JavaScript.
Gzip helps reduce the impact of this, but Styletron's gzipped CSS is still much (40% or so) smaller than the other CSS-in-JavaScript implementations [2]. In some cases, the cost of switching to using a tool like Styletron might be justified by faster loading times in low-bandwidth markets.
> We must deliver the absolute minimum amount of CSS necessary by only sending down CSS in use by components in the server-side rendered body. This rules out extracting all the CSS used in all of our components at compilation time, as much of that CSS would not be needed for the initial page load.
Colocating styles with components is a win, but, more importantly, styles-as-JS-objects was the simplest way to get this perf feature out the door.
Duplicated styles also compress better with gzip as you said. Do you think stripping a few kbytes is worth turning your whole process upside down?
The point of CSS-in-JS and CSS modules isn't that it's necessary, just that it makes avoiding these sorts of problems much easier.
Absolutely right. I covered this in a blog post I wrote over a year ago (which still applies today): http://hugogiraudel.com/2015/06/18/styling-react-components-...
Basically, it seems like everyone is talking about how amazing it is and how much it improves their productivity, and this makes me feel like there must be something I'm missing but I can't figure out what that is.
React's biggest advantage is really not the virtual DOM, but rather how it enables you to think about your application. The phrase "easy to reason about" is kind of overused by now, but there's a lot of truth to it. React encourages use of small components that can be composed together as needed, top-down data flow, and Functional Programming principles. Also, its rendering approach lets you think almost exclusively in terms of "given this state data, what do I want my UI output to look like?", rather than "_If_ this div was visible, I want to toggle it off when there was a click here, but only if..."-type transitions. Overall, that makes it a whole lot easier to understand what is going on inside the application.
There was a recent Reddit thread labeled "What were the biggest 'aha' moments you had in learning React?" ([0]), which has a lot of good comments. You might also be interested in reading through some of the posts on "Thinking in React" ([1]) out there, as well as some business case arguments for using React ([2]). Both those sections are part of my larger React/Redux links list ([3]), which has links to numerous tutorials, articles, and other resources on React.
[0] https://www.reddit.com/r/reactjs/comments/5gmywc/what_were_t...
[1] https://github.com/markerikson/react-redux-links/blob/master...
[2] https://github.com/markerikson/react-redux-links/blob/master...