If using create-react-app then:
1)First install sass dependency using npm:
npm install sass --save-dev
2)Import your sass file to your componentName.js file
import '../scss/FileName.scss';
If using create-react-app then:
1)First install sass dependency using npm:
npm install sass --save-dev
2)Import your sass file to your componentName.js file
import '../scss/FileName.scss';
The way to use scss depends a bit on your React development environment. For beginners React recommends using Create React App which is, according to them, "a comfortable environment for learning React, and is the best way to start building a new single-page application in React." You can read more about it at https://reactjs.org/docs/create-a-new-react-app.html. To create your app you simply type the following at the command line:
npx create-react-app my-app
After that, React sets up a full development environment with css files you can edit to style your code.
If you want to continue using create-react-app (sometimes called CRA) and use scss then you can install the Dart Sass library by typing:
npm i sass --save-dev
(Keep in mind that node-sass in deprecated and we are using Dart Sass instead of it)
For a full explanation about how to use node-sass and CRA together see "How to Use SASS in Create React App?": https://www.robinwieruch.de/create-react-app-with-sass-support
Once you move beyond CRA you can tinker with your own webpack.config.js which can also be set up to compile and import SCSS files. But if you are just starting out with React then you may want to leave tinkering with your webpack.config.js for later and stick with CRA.
Videos
Hey,
So far in my React projects I've been putting all my Sass files in a "styles" folder inside src. Basically I import the files that correspond to each component into the main Sass file that's then compiled to CSS. But I get why this wouldn't be a scalable solution, as the number of files grows it could get confusing to know which Sass file corresponds to which JS component.
I know there's styled components, which is definitely something I'm going to try in my next project, but for the current one I want to use Sass. So I tried using CSS modules but it's really tedious to have to import every Sass class as a variable into the JS component. Unless I'm missing something.
I guess another alternative to what I've been doing would be to have the Sass files in the same folder as their respective components and then still import everything into the main Sass file. But for some reason this doesn't feel right, so is there a better way to use Sass in React?
lorem ipsum
I want to learn the folder structure, code writing conventions etc. Any help or resource will be appreciated :)
Feel free to DM as well.