Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

importing sass variables from jsx file

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.

like image 705
Mister_L Avatar asked Sep 12 '25 10:09

Mister_L


1 Answers

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}} />
like image 155
llobet Avatar answered Sep 14 '25 23:09

llobet