Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to use scss in ReactJS

Introduction

Welcome to this comprehensive guide on how to use SCSS (Sassy CSS) in ReactJS. If you're learning programming, you may have come across CSS (Cascading Style Sheets) - a stylesheet language used for describing the look and formatting of a document written in HTML. But you might be wondering, what is SCSS and how does it relate to CSS?

In simple terms, SCSS is a CSS preprocessor, meaning it's a scripting language that extends CSS by providing additional features which aren't available in CSS itself. Think of it as a tool-kit that helps you write more efficient and maintainable CSS.

Setting Up The Environment

Before we dive into the details of using SCSS with ReactJS, we need to set up our environment. Here's how to do it:

# Create a new React project
npx create-react-app scss-react-app

# Move into the project directory
cd scss-react-app

# Install node-sass
npm install node-sass --save

The node-sass library allows us to compile .scss files into .css files in our React application.

Writing SCSS

Now that our environment is set up, let's write some SCSS. In your 'src' folder, create a new file called 'App.scss'. This is where we will write our SCSS code.

// Define a variable
$primary-color: blue;

// Use the variable
body {
  background-color: $primary-color;
}

In the above code, we're defining a variable $primary-color and assigning it the value blue. We then use that variable as the value for background-color in our body selector.

Importing SCSS Into React

To use our newly created SCSS file in our React component, we need to import it. Open 'App.js' and add this import statement at the top:

import './App.scss';

Now, the styles defined in 'App.scss' will be applied to our React application.

Nesting in SCSS

One of the features that make SCSS powerful is nesting. This allows us to write cleaner and more readable code. Here is an example:

nav {
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  li { 
    display: inline-block; 
  }

  a {
    display: block;
    padding: 6px 12px;
    text-decoration: none;
  }
}

In the above code, we are nesting ul, li, and a inside the nav selector. This represents the same structure as writing this in CSS:

nav ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

nav li { 
  display: inline-block; 
}

nav a {
  display: block;
  padding: 6px 12px;
  text-decoration: none;
}

Using Mixins

Mixins are a way to define styles that can be re-used throughout the stylesheet. They're kind of like functions in JavaScript. You can even pass in parameters. Here's an example:

// Define a mixin
@mixin transform($property) {
  -webkit-transform: $property;
  -ms-transform: $property;
  transform: $property;
}

// Use the mixin
.box { @include transform(rotate(30deg)); }

In this example, we define a mixin called transform that takes one parameter $property. When we include the mixin in .box, we pass in rotate(30deg) as the argument.

Conclusion

SCSS is a powerful tool that can make writing CSS more efficient and manageable. With features like variables, nesting, and mixins, you can write cleaner, more readable, and DRY (Don't Repeat Yourself) code. By setting up your environment correctly and understanding how to use these features, you can take full advantage of SCSS in your React applications.

Remember, learning to code is like learning a new language. Practice and patience are key. The more you use SCSS and ReactJS together, the more comfortable you'll become. So keep coding, and most importantly, have fun doing it!