3

What is the best way to handle shared scss file in react? Assuming I have the following file

// variables.scss
$primary: rgb(1,2,3);
$secondary: rgb(2,3,4);

And I have multiple components who need to use those colors. of-course every component has its own scss file and I import this file in component jsx file, do I need to import this file multiple times in every component who need to use those variables?

If I do import this file in every component scss file does webpack with sass-loader know to handle it and import it once?

UPDATE: To explain the question better this is my issue: I have a component A this is the jsx file

// A.jsx
import React from 'react';
import './A.scss'

//more code here...

A scss looks like this:

//A.scss
import '../../variables.scss';

and I also have component B this is the jsx file

// B.jsx
import React from 'react';
import './B.scss'

B scss looks like this:

//B.scss
import '../../variables.scss';

Now as you can see both A.scss and B.scss imports the variables, is this the right way? If yes does webpack sass-loader know not import the same file twice? If not what is the right way?

6
  • You want to use $primary(eg) in jsx/scss files? Commented May 17, 2018 at 9:47
  • @Raviteja in the scss files yes. Commented May 17, 2018 at 10:58
  • stackoverflow.com/questions/17598996/… Commented May 17, 2018 at 11:03
  • @Raviteja Thanks. I know this solution but in react I import the component scss file inside the component scss so there is no need for a base file which imports all the files, because jsx already does that Commented May 17, 2018 at 11:32
  • What do you need then? Commented May 17, 2018 at 11:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.