5

I think it might be silly question to ask but trust me I am in trouble and frustrated . Actually I have two components one is Home and second is Dashboard . I created two sass file for individuals components mean one for home and second is for dashboard. For Home it working fine but when I include sass file into dashbaord component then it break home page component style . How to fix this issue . I want to implement it separately, it should be independent.

I also used react-router so please help me to achieve my goal

10
  • how are you importing it? Commented Oct 15, 2019 at 17:59
  • import "./dashboardStyle.scss"; Commented Oct 15, 2019 at 18:05
  • how are you importing your sass file in home component? Commented Oct 15, 2019 at 18:08
  • @CecilJohnTantay I created fiddle, please have a look ( code ) Commented Oct 15, 2019 at 18:13
  • jsfiddle.net/chtkus42/1 Commented Oct 15, 2019 at 18:13

2 Answers 2

2

You can implement independent sass file to react component by using Sass css modules.

You have to create two sass files Home.module.scss and Dashboard.module.scss

.btn {
  color:red;
}

You can import like this .

import {btn} from "./Home.module.scss";
import {btn} from "./Dashboard.module.scss";

where btn is the class name you have in sass files.

Then you can use that class in component like this .

<a className={btn}> Button </a>

This way you can use sass file independently . Let me know if need more explanation .

Sign up to request clarification or add additional context in comments.

12 Comments

How to import all classes from Sass file ?
You can do like this import {class1, class2, class3} from "./Home.module.scss";
Or you can use destructuring .
if I have thousand class in file then I will do like this ?
import styles from "./Home.module.scss"; Then use like this. <a className={styles.btn}> Button </a>
|
1

I'm going to assume you installed sass by installing node-sass.

All you need to do is to import them into a component that needs sass at the top of the js file.

import './dashboard.scss'

Comments

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.