2

I create my app with create-react-app and I try to use shared scss files. I have folder with them files common/styles/. And also I have some scss files there (_reset.scss, _variables.scss).

My _variables.scss:

$color-main: #1E700F;

My index.scss:

@import "variables";

Then I connect my index.scss and my index.js file

import React from 'react';
import ReactDOM from 'react-dom';
import './common/styles/index.scss';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));

Then I create Button component in folder common/components/Button. And I want to use in my Button.scss next code:

.button {
  border: 1px solid $color-main;
  color: $color-main;
}

After I import my Button.scss to Button.js file I have next error:

SassError: Undefined variable: "$color-main".
2
  • When writing mixins and functions that take arguments, you usually want to ensure that those arguments have the types and formats your API expects. Commented Feb 1, 2020 at 11:12
  • you may get a solution on simple googling. try that first. we will help you if you get any issue Commented Feb 1, 2020 at 11:22

1 Answer 1

2

You shouldn't be importing scss in the js file. Instead create a sub folder inside your scss and name it components. Then create a scss file specific for such component which is "button" for you. Then import it like this in your index.scss

@import "variables";
@import "reset";
@import "components/button" <-- your button scss should be under the variables
Sign up to request clarification or add additional context in comments.

2 Comments

I guess This will add the all css in the final bundle, which is a bad practice. is there any other way to prevent this?

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.