0

I have a stylesheet for a button which is located at styles/Button.module.css and i am trying to use it in my NextJS project but can't seem to get it going.

In my component, i have imported the stylesheet normally, i.e;

import Button from '../styles/Button.module.css';

Then i am trying to add it to the return in a component, like so;

<button onClick={e => login(e)} className={"text-white bg-indigo-600 " + (loading ? "Button.btn-loading" : "")}>
    Login
</button>

But the styles don't seem to be pulling through.

Any help would be greatly appreciated

1 Answer 1

1

"Button.btn-loading" is invalid. Button is an imported JS object, hence you need to remove the quotes to evaluate the JS object notation.

<button onClick={e => login(e)} className={"text-white bg-indigo-600 " + (loading ? Button['btn-loading'] : "")}>
    Login
</button>

That's why CSS Modules recommends using camelCase class names, so that we can use dot notation instead of bracket notation to get the values.

I'd suggest using template literals for more cleaner syntax

<button onClick={e => login(e)} className={`text-white bg-indigo-600 ${loading ? Button['btn-loading']: ""}`}>
    Login
</button>
Sign up to request clarification or add additional context in comments.

3 Comments

Hey, I am getting a btn is not defined error now
I manged to sort that error out by putting quotes around the class, like 'btn-loading' ... Thank you for your help!
Oh you're right, you need quotes inside a bracket notation. I've edited the answer. Thanks to you too!

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.