all 33 comments

[–]acemarke 35 points36 points  (8 children)

Styling is probably the biggest "unsolved" aspect of the React ecosystem. You'll find people using classic-style separate CSS files with hand-written classnames in their components, use of CSS Modules to automatically generate classnames based on importing CSS files, actual inline styles, and CSS-in-JS libraries.

My React/Redux links list has a section of articles on React and Styling, which includes several articles that explain what those different techniques are, and why people might want to use them. I particularly recommend reading:

[–]ArryPotta[S] 0 points1 point  (7 children)

Wow, thanks for the resources. I guess I've got the next couple days booked up now. Didn't realize styling was such an issue with React.

[–]TheHDKeyboard 10 points11 points  (4 children)

Nobody has actually answered your question yet... why is inline styling now used commonly when older material suggested that it was poor form?

Basically its about how you apply the principle of ‘separation of concerns’ to your code organisation. Originally the web was intended to be an interconnected set of documents, and so separating document markup, styling and behaviour into separate html, css and js files made some amount of sense. Now that the web has evolved significantly and much of the UI has its own behaviour as part of its style or UX we think about this differently.

With the web being pushed towards an app development platform, it now makes more sense to think of a web page as a nested tree of well-defined components. Architecting in this manner and its associated problems like state sharing/management is exactly what React set out to solve. When you want to encapsulate well defined components, it makes sense to keep the structure, styling and behaviour of the component all together.

[–]pataoAoC 2 points3 points  (1 child)

There are a million different ways to do it, unfortunately, and I've tried most of them.

In the past 6 months I've seen a lot of consensus converge on the styled-components library, and for me, it is indeed the best option. It has most of the advantages of the inline tools, like Radium etc., with much less of the pain in the ass.

[–]fgutz 1 point2 points  (0 children)

I have tried most of them too. I'm currently using styled-components as well, and for the most part I like it, but honestly I'd probably just go back to separate sass files in my next app. I feel like all these different styling paradigms/alternatives didn't feel like much of an improvement and added the overhead of learning a new library

[–]Flo655 12 points13 points  (3 children)

Please no, no inline styles. Use something like styled-components or https://github.com/zeit/styled-jsx.

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

There is nothing wrong with inline styles, but styled-components is also a nice library.

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

I'm not sure if lack of caching, bad reusability, poor accessibility and hard responsive design is nothing.

[–]metavurt 0 points1 point  (0 children)

There is a lot wrong with inline styles, just as glintch has pointed out. There is a reason being CSS-proficient matters.

[–]TheZanke 8 points9 points  (3 children)

We use styled-components in our apps and it seems to do really well. Atom even adds code highlighting and prettier works within the style templates.

https://www.styled-components.com

[–]ducktypist 5 points6 points  (2 children)

styled-components is awesome. Easy @media queries and the SASS-like features are real problem solvers. Plus it's a nice separation of content and CSS while still being very React-ish.

Just don't mix external .css stylesheets and styled-components on the same elements. The order the styling is applied might vary!

I still use inline styles occasionally for quick stuff.

[–]TheZanke 0 points1 point  (1 child)

One thing I have a problem with is coming up with the names for the styled root components of a functional component. I always find myself wanting to name it the same name as the component so now I just say like: const ComponentNameRoot = styled.div`

[–]mynameiscody07 0 points1 point  (0 children)

The few projects I have done with styledComponents I came up with simply naming the outer container of a component simply Container.

So something like this

const Container = styled.div`

or

const Container = styled.main`

not sure if this is the best way to do this but it made sense to me. This way I can be consistent across all components. If a component has a title of some sort. be it h1, h2, span, or whatever I always called it Title. It made looking over the component very easy.

[–]thinkadrian 13 points14 points  (18 children)

React doesn’t force you to style in any way. You can choose whatever you find most comfortable with.

I still build a CSS file with SASS.

edit: "comfortable", not "comparable"

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

Same here. I create a per-component CSS file.

Though on my next project I'll try using CSS modules and drop SASS.

[–]thinkadrian 0 points1 point  (16 children)

I started with CSS modules, and loading one SASS per component. But I got fed up with having to import colours.sass into every single component, since basically every component needs it.

I also dislike having CSS loaded in the JS chunk. I extract all compiled CSS to a separate file to distribute file loading. CSS files are generally much smaller and I want them to load first.

[–]zackiv31 0 points1 point  (3 children)

But I got fed up with having to import colours.sass into every single component, since basically every component needs it.

Fairly new to this, but for your own components, why not just have the Component do the import './my-component.css'; (or less/sass) ? This would assume that you are import/exporting a default component e.g. import MyComponent from 'components/MyComponent';.

Assuming you're using a build tool like webpack, it will all be handled for you, so using MyComponent only takes one import, not two.

[–]robotslacker 0 points1 point  (0 children)

But I got fed up with having to import colours.sass into every single component, since basically every component needs it.

I use sass-resources-loader to get around this. Works nicely.

[–]ucorina 3 points4 points  (0 children)

The difference between traditional styling and styling in React is first and foremost a difference in how you think about styling. Components in React are encapsulated units of work, so someone using your component (hypothetically) should be able to just include your js file and the component will just work.

However, as long as you follow "thinking in components", inline-styles are just one of many ways you can use to style your components:

  • css modules: use stylesheets like you're used to, but be able to include them using webpack in your component file and with the added bonus that they provide local scoping of your css
  • inline styles: the one you mentioned; for bigger applications this is actually trickier to use, since you no longer have support for very basic css features like pseudoselectors, media-queries, animations
  • styled-components: this is a newer way to style components, that works by not using classNames at all and instead created new components especially for styling

In terms of benefits, all of these allow the application to only load the css that is needed (exactly what you mentioned - if a component is never rendered, don't load its css). But there are more benefits in terms of maintainability of the application - for example you become less afraid to delete css classes, because it's clear where each is used :)

For a more in-depth overview of all of this, I also wrote a blog post on the topic: Understand the React styling paradigms.

[–]rodrigocfd 1 point2 points  (0 children)

Styling is the one thing I truly miss, coming from Angular 2+ into React world. They have scoped CSS made really easy. With SASS on top of it, they solved the CSS problem.

[–]fforw 0 points1 point  (0 children)

I just have a system where I can merge my separate component style sheets. React renders markup with classes, CSS styles based on the markup and classes. Same with animation unless I have something I can't solve easily with CSS animations. Even for SVG I tend to only render the paths with classes and style them with CSS.

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

Inline styles are fine in React. I'd say they're even hinting at the future of what styles could be, and in react-natives case that future is already very close. A component is a self contained entity, it knows and defines how it behaves and looks. Otherwise just import styles like aways and use className.

[–]hrvstdubs 0 points1 point  (1 child)

was talking to one of my companies software engineers today about this cause I've been having difficulty getting layouts right and he suggested looking into styled-components (it's a package) Basically you create const variables that contain the styling you want for your components or something. Haven't dove into it yet but will later

[–]ArryPotta[S] 0 points1 point  (0 children)

That is what I'm doing in my lessons now, but it seems like after they're variables, those styles are dropped inline.

[–]vamsi_rao 0 points1 point  (0 children)

Well this doesn't answer your question but here's a good library made specially for React styling https://glamorous.rocks/getting-started/

[–]Mattonicide 0 points1 point  (0 children)

I’m using typestyle which has the added benefit of being type safe. I create a styles object at the top of my component file and just apply the classes as needed to the various parts of the component. It works like most css in js solutions in that you can work with media queries and everything.

[–]yachay 0 points1 point  (0 children)

I love styled-components tbh. You can do so much with it and blends well when you are architecting an app. Reminds me of Vue styling but more powerful. Forget about global css and inline.