0

In my nextjs-app, I have a component where I want to add conditional styling.

So I have made a component-level stylesheet MyComponent.module.css, which has:

.primary {
  color: blue;
}

.secondary {
  color: orange
}

Then, in my component, I tried to do this:

import styles from "./MyComponent.module.css"

export default function MyComponent({ text, type }) {

  return (
    <button className={`${type === "primary" ? styles.primary : styles.secondary}`}>
      {text}
    </button>
  )

}

but this doesn't work e.g. returns undefined

So, how can I solve this?

1 Answer 1

2

Try removing the backticks.

import styles from "./style.module.css";
const App = () => {
    const type = "primary";
    const text = "Hello"
    return (
        <div className={type === "primary" ? styles.primary : styles.secondary}>
            {text}
        </div>
    );
};
export default App;
Sign up to request clarification or add additional context in comments.

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.