How to use sass stylesheet in react app

How to use sass stylesheet in react app

In this tutorial, we are going to learn about how to add and use sass(syntactically awesome style sheets) to your create react app with the help of an example.

What is Sass?

  • Sass is a css preprocessor, which allows us to use features that don’t exist in CSS like nesting, mixins, inheritance, etc.
  • Sass compiler complies your sass files into css files.

Adding sass to react

To use sass in react we need to install a new package called node-sass from the npm.

This tutorial assumes that you already created a new react project using create-react-app cli.

Run the below command to install node-sass.

npm i node-sass

Now, we are ready to use sass in our react app without ejecting.

Using sass

Now, rename your .css files into .scss and also change your css imports to scss.

Example:

index.scss
$font-stack: Helvetica, sans-serif;
$primary-color: #333;

body {
  font: 100% $font-stack;
  color: $primary-color;
}
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.scss';import App from './App';


ReactDOM.render(<App />, document.getElementById('root'));