Hacker News new | ask | show | jobs
by kowdermeister 3484 days ago
It makes no sense for 99.9% of the apps out there. If you are running at Facebook or Google level, maaaybe. This whole madness started with a FB dev presentation about running CSS "at scale". Then folks started to copy each other and this is what we have now.

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.

2 comments

I think the argument is that separation of UI components is a more true "separation of concerns" than separating CSS, JS, and HTML.

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...

> the component is easier to manage

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.

An additional problem is that even when CSS is properly scoped, you end up with wasted bits over the wire by sending down duplicated declarations. I did some cursory research using cssstats [1] because I was curious how many CSS declarations are unique. Turns out it's about 20-25% for most "big tech" websites.

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.

[1] http://cssstats.com

[2] https://ryantsao.com/blog/virtual-css-with-styletron

Bandwidth was the major motivation for building Aphrodite, too: Khan Academy wanted to decrease the number of bytes required for the initial pageload. http://engineering.khanacademy.org/posts/aphrodite-inline-cs...

> 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.

Faster load times is bit of a stretch. While it's true that up to 40% sounds good, but we are talking about a few kbytes here meanwhile images penalize you much much more on load time. And what do we sacrifice? Mixing concepts that shouldn't be mixed in the first place.

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?

You're absolutely right that you can do it with pure CSS. But doing a pure CSS solution usually involves some CSS methodology that you need to apply extremely strictly and consistently, which honestly can be a challenge, especially in large apps. Sometimes it's easier to just throw in a new selector at the end of your stylesheet and "make it work". Without discipline, things can get messy pretty quick.

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.

> IMHO SASS covers almost all cases of CSS "at scale".

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-...

TBH React itself is another thing I don't quite "get". Isolated components make sense when you have a big webapp like Facebook with a lot of isolated parts, but everything I read about it encourages slicing your code into lots of teensy little modules (which you then have to keep track of, and frequently are used only once), and then goes on about how the amazing Shadow DOM keeps everything fast despite this massive abstraction. Meanwhile I have my view model(s) plus a single big HTML document with bindings in it (and sometimes a few includes for particularly complex bits), bound together with Knockout, and I've never noticed this monolithic approach causing any problems.

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.

Couple corrections. React uses a "virtual DOM", which is a tree of plain JS objects that can be diffed between renders. "Shadow DOM" is a browser feature related to the Web Components spec.

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...

[3] https://github.com/markerikson/react-redux-links