liveBook · Manning

CSS-in-JS

As explained in React in Depth

CSS-in-JS is a technique for defining, composing, or generating CSS styles from JavaScript code.

CSS-in-JS is a family of approaches for writing CSS inside JavaScript files. In React, JSX already keeps a component’s markup close to its logic; keeping styles nearby can make changes to all three easier to coordinate. It is an option for teams encountering friction with separate CSS, not a requirement for every project. section 7.1 section 7.7

The choice turns on which problems matter in your application:

  • Separate CSS files and class names are familiar and need little extra training, but provide no guarantee that a component uses the classes its stylesheet defines. Misspellings and unused classes can go unchecked. section 7.3.5 section 7.7
  • Global class names can collide across components or with a host website. Dynamic JavaScript values may require inline CSS, and rule ordering can complicate specificity. section 7.3.5
  • CSS Modules preprocess regular CSS into unique generated class names. They reduce collision risk without abandoning CSS files, but do not address every weakness of separate files. section 7.7
  • Styled-components places CSS beside the components that need it, giving a useful at-a-glance view for small components. That advantage diminishes as components grow. section 7.7

A component and its stylesheet

Consider a Button.js component importing button.css. The import does not verify that the component’s class names match the stylesheet. A misspelling can leave a style unused, while a generic class such as .group can unexpectedly affect another component. Keeping styles closer to the component addresses the coordination problem, but the appropriate styling approach still depends on project and team needs. section 7.3.5 section 7.1

Choose for the costs you actually face. Plain CSS remains reasonable for smaller projects; CSS Modules offer a modest change; styled-components can improve the overview of medium-to-large projects, though it is not a universal recommendation. section 7.7

How React in Depth covers it

How deep it goes
Explains the motivation and compares styling approaches and failure modes, without showing a CSS-in-JS implementation in the supplied excerpts.
Examples
Uses a Button.js/button.css mismatch, a .group class collision, and a JavaScript width value to illustrate styling friction.
What it assumes
Assumes familiarity with React components, JSX, CSS files, and class names rather than teaching those basics from zero.
What you can do after
Evaluate/understand it when choosing among plain CSS, CSS Modules, and styled-components for a React project.
How it connects
Connects JSX and React components to CSS class names, CSS Modules, and styled-components.

Also explained in

Compare how 3 books explain CSS-in-JS →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React in Depth. The section links point to the parts of the book it draws on.