跪拜 Guibai
← All articles
Frontend

The Sass Fundamentals That Power Most React Admin Stylesheets

By Kevin666 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Native CSS custom properties handle theming, but Sass's compile-time nesting, mixins, and module system remain common in React codebases. Knowing the four core features lets a developer read and maintain existing `.scss` files without treating Sass as a separate language.

Summary

Sass extends CSS with compile-time features that cut repetition. A single `$primary-color` variable replaces a hard-coded hex value scattered across a stylesheet. Nested selectors mirror the DOM hierarchy, `&` gives access to the parent selector for hover and state variants, and `@mixin` bundles reusable style blocks that can accept parameters.

The article walks through the CSS-versus-SCSS-versus-Sass distinction: `.scss` uses braces and semicolons so it looks like CSS, while the indented `.sass` syntax drops them. Real projects favor `.scss`. Sass compiles to CSS before any browser touches it, either through the `npx sass` command or through a build tool like the one in a React project.

For modular styling, `@use` imports other Sass files, replacing the deprecated `@import`. The article's priority table marks variables, nesting, and `&` as P0 for React work, mixins and `@use` as P1, and advanced features like `@extend` and loops as P2. A 30-minute study of the first three is enough to understand typical admin project code.

Takeaways
— Sass compiles variables, nested selectors, mixins, and `@use` modules into plain CSS that browsers can parse.
— `.scss` syntax uses braces and semicolons like CSS; `.sass` is indentation-based and less common.
— The `&` in nested rules refers to the parent selector, enabling `&:hover` and `&.active` patterns.
— Mixin definitions use `@mixin`, and instances use `@include`, with support for default and explicit parameters.
— Install Sass with `npm install -D sass`, compile with `npx sass style.scss style.css`, or import `.scss` directly in a configured React build.
— `@use` imports other Sass files and namespaces their variables; `@import` is deprecated.
— In a priority table, `.scss`/`.css` differences, `$variables`, nested selectors, and `&` are P0; `@mixin`/`@include` and `@use` are P1; `@extend`, `@function`, `@each`, `@for` are P2.
— A typical React business component uses nested selectors, `&:hover`, and `&.active` in a card or list item.
Conclusions

The article's priority table reflects real React admin work: only a handful of Sass features appear in everyday stylesheets, while advanced constructs like loops and functions remain marginal.

Native CSS custom properties and nesting have closed part of Sass's gap, but Sass's mixins with parameters and `@use` modularization still solve problems CSS alone cannot.

Concepts & terms
Sass
A CSS preprocessor that adds variables, nesting, mixins, and modularity; compiles to plain CSS.
SCSS
The most common Sass syntax, using curly braces and semicolons like CSS; files use `.scss`.
Mixin
A reusable block of CSS code defined with `@mixin` and included with `@include`; can accept parameters.
`&` parent selector
In nested rules, `&` stands for the current parent selector, enabling states like `&:hover` or compound classes like `&.active`.
`@use`
Sass module directive that imports another Sass file and gives its variables a namespace; recommended over the deprecated `@import`.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗