0

This is my SCSS style for .br

 .br {
          &--red {
            border: 2px solid red;
          }
          &--green {
            border: 2px solid rgb(32, 170, 8);
          }
        }

This is my nextjs react code:

import layout from "../styles/layout.module.scss";

export default function Index() {
  return (
      <div className={layout.br}>
        <h1>Khan</h1>
      </div>
  );
}

How to access this ".br--red" in jsx?

.br{ 
    &--red{
        border:2px solid red; 
          }
}
Adnan Sparrow
  • 25
  • 2
  • 8
  • Does this answer your question? [How do I reference a JavaScript object property with a hyphen in it?](https://stackoverflow.com/questions/7122609/how-do-i-reference-a-javascript-object-property-with-a-hyphen-in-it) – juliomalves Nov 15 '21 at 23:06

2 Answers2

2

If you use dash in an object's properties name(in this case, it's class name), for use of it you must use Bracket notation, and your property name should be in ' '(quotation or double-quotation).

<div className={layout['br--red']}>
    <h1>Khan</h1>
</div>

You can read more about it in this article.

Zahra Mirzaei
  • 729
  • 3
  • 7
0

Adding on the comment above, you can also use dynamic variable in the brackets like so using backticks.

className={styles[`br--${color}`]}
Im24n
  • 61
  • 1
  • 3