3

I'm working on a React project built with create-react-app. I have a file called common.scss where I keep a list of variables, for example:

$blue: 'blue';
$red: 'red';

The SCSS files are pre-processed with node-sass-chokidar.

I would like to be able to import these variables into my jsx files, but I could not find a clear way of doing this without ejecting. Can you show me a way to do this without eject?

Thanks.

Mister_L
  • 2,469
  • 6
  • 30
  • 64

1 Answers1

2

If you use CSS modules, you can import a css file with some variables exported.

$blue: 'blue';
$red: 'red';

:export {
  blue: $blue;
  red: $red;
}

And then use them as js.

import css from 'path/to/your/file.scss'
/*
...
*/
<div style={{color: css.blue}} />
llobet
  • 2,672
  • 23
  • 36