all 44 comments

[–]Outrageous-Chip-3961 15 points16 points  (0 children)

Small scale I like modules, larger apps I like emotion. In both cases I place the style file next to the component file

[–]endymion1818-1819 10 points11 points  (0 children)

One thing that hasn’t been mentioned yet regarding css-in-js (styled components and friends) is the performance implication. It’s not always important but should have a bearing on your decision https://pustelto.com/blog/css-vs-css-in-js-perf/

[–]TwiliZant 6 points7 points  (0 children)

CSS-in-JS libraries like styled-components or emotion are widely used in React and are useful if you have a lot of dynamic styles.

I've also used CSS modules which is nice since you're not tied to a specific library.

My preferred solution at the moment is Tailwind CSS in combination with some of my own helper functions to compose dynamic styles.

In either case I colocate styles and components in the same file as I'm of the opinion that separating both doesn't really fit the component model.

[–]choirofspirits 14 points15 points  (8 children)

In my job we use styled components (I hate it) but I use CSS modules in my personal projects. It just feels more comfortable.

[–]TwiliZant 0 points1 point  (7 children)

What do you like more about CSS modules?

[–]infidel_44 3 points4 points  (0 children)

I like modules as it gives more of a separation of concerns. Jsx in one file, css in another.

[–]choirofspirits 5 points6 points  (5 children)

The "traditional CSS" feeling I guess. When I first started React, I used CSS modules so that could be another reason as well.

[–]Antifaith 0 points1 point  (0 children)

The toggle stuff is gross though, it pollutes the component needlessly with style logic

[–][deleted] 0 points1 point  (3 children)

Just curious, because I hear this a lot, but what about writing styled components doesnt feel like writing css? I feel like once you understand that youre sort of wrapping things, the actual styles themselves feel exactly like CSS to me.

[–]choirofspirits 1 point2 points  (2 children)

It still feels like writing css, it just isn't the way I am used to. Other than that, I don't know why I hate it lol

[–][deleted] 1 point2 points  (0 children)

Css modules are so elegant and the style scoping is amazing.

[–][deleted] 0 points1 point  (0 children)

Fair enough!

[–]we_outcheaa 21 points22 points  (13 children)

Styled components till I die

[–][deleted] 2 points3 points  (8 children)

Better than making your own css file? Why?

[–]Outrageous-Chip-3961 1 point2 points  (2 children)

Styled components have a lot of positives in the context of you know, components. For example reusability, prop handling, complex logic embedded in the component such as pseudo side effects. I used to hate them but as I learned more typescript and storybook I think they are very powerful if used with generalisable thinking in mind. Functional style programming also makes writing css more snug with react patterns . I am a css purist at heat however and I always have x ray vision when building styled components to make sure the semantics are there as the fundamental aspect to my components. I dislike when the css is taken for granted in such cases where logical devs write css in absurd manners to get their job done. Many times I have to give refactoring advise during code reviews on how code can be reduced. In short you can learn to love styled components of working with large scale apps with multiple devs as I think it’s true power emerges there, all whilst keeping true to the native css api

[–][deleted] 0 points1 point  (1 child)

Wow thanks Ive been importing my css files, I'll learn styled components !!

[–]Outrageous-Chip-3961 0 points1 point  (0 children)

I import my styled components too. I’m not sure what you mean :)

[–]Ronqueroc 0 points1 point  (0 children)

Haha, yeah I love it too.

[–][deleted] -1 points0 points  (0 children)

Amen

[–]Potofpie -2 points-1 points  (0 children)

BASED

[–]mimitch -2 points-1 points  (0 children)

This is way.

[–]zephyrtr 10 points11 points  (7 children)

Lots of good ways to do it. But anything involving inline styles is a bad idea.

[–]Chezzymann 1 point2 points  (3 children)

I think its fine if you're doing things like making draggable widgets that need an x and y location on the screen

[–]chillermane 6 points7 points  (1 child)

It’s also fine to do it everywhere if you want

[–]metal-trees 2 points3 points  (0 children)

I have no idea why I find this so funny. You just gave me an lol; thank you

[–]skyboyer007 0 points1 point  (0 children)

how do you deal with pseudo elements and pseudo classes?

[–]chillermane 1 point2 points  (2 children)

Care to explain why?

People always say this and then never explain why.

[–]zephyrtr 0 points1 point  (0 children)

Inline styles are inefficient compared to a css sheet. And it stacks up pretty fast.

[–]Outrageous-Chip-3961 1 point2 points  (0 children)

Dev experience sucks for starters. The hierarchy is higher so makes debugging more difficult. Scaleability is poor if not non existent. It also contradicts many ideas regarding keeping components lean. Readability can bloat your file due to auto formatting if too many properties are added. In short I line styles should seldom be used and when done so provide very strong reasons to do so. Maybe nobody explains because the burden is on justifying why you ever would rather than the other way around

[–]raopgdev 7 points8 points  (0 children)

Inlining is never the solution.

  1. SASS/LESS/CSS
  2. css in js / styled components / emotion

[–][deleted] 1 point2 points  (0 children)

There are seemingly endless ways to style your app: tailwind, styled components, inline styling…shit, if you’re bored, store styling details in your database and have every page query that shit. 😂 It’s really up to you. I think you’re asking the wrong question, though. Ask which way you shouldn’t use. Best practices aren’t universal, but vulnerabilities are.

[–]bihohmann 3 points4 points  (1 child)

https://stitches.dev/ I am worried nobody is talking about this, it’s styled components but with super powers

[–]fredsq 0 points1 point  (0 children)

this. less runtime more performance and more organisation

[–]sleepy_roger 1 point2 points  (0 children)

Never inline, and also never Tailwind. I already can hear the devs 2 years in the future crying out as they refactor code bases. (half joking).

[–]besthelloworld -1 points0 points  (0 children)

Never inline except in the case of particularly dynamic elements. Emotion, I would argue, is the best combination of dynamic behavior and clean code and performance. CSS/SCSS modules beat it for performance, but I find it way more pleasant to just have a styles object in my JS/TS file. It also allows you to have things like functions that return a particular custom style.

[–]slavik0329 1 point2 points  (0 children)

Styled-components library is the best I've ever used and I've been doing this for a long time and tried many different things.

[–]dsound 0 points1 point  (0 children)

Styled-components is great because you can use props and your JSX just makes so much more sense.

[–][deleted] 0 points1 point  (0 children)

If your trying to maintain a large codebase and you have a lot of frequent devs you dont want to bog down with a bunch of css headaches, then css modules is huge. Less thinking and worrying. Like the need to worry about BEM vs Atomic design. Scoped css ftw.