1

I'm trying to reference a color variable from my main.scss file within my component but not sure what the correct syntax is. Right now it works by hard coding the color hex value.

I can make it work by adding an external stylsheet, and referencing the theme color that way however don't want to create an extra file but rather use internal styling.

Here's main.scs:

$theme-colors: (
  'primary': #00a677,
);

Here's the component:

import React from 'react';

const box = {
  color: '#9D2235',
};

const Box = () => {
 return(
  <div style={box}><h1>I'm A Box!</h1></div>
 )
}

export default Box;

Any idea how I can do this?

1 Answer 1

1

What you can do is, put a class on the box component

In main.scss

$primary: #00a677

.box {
   color: $primary
}

In your component

import React from 'react';

const Box = () => {
 return(
  <div className='box'><h1>I'm A Box!</h1></div>
 )
}

export default Box;
Sign up to request clarification or add additional context in comments.

2 Comments

That is an option, but I really wanted to separate the main styling from component specific styling.
@EricNguyen Do you mean is you have main.scss that contains all of the styling variables and you also have separate scss for the components? If that's the case, you can store all your styling variables to main.scss and just import main.scss to the other scss that being used by the component you wanted to use the variable. You may refer to this sass documentation sass-lang.com/documentation/at-rules/import

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.