0

I'm trying to make a mobile drop down for a React web app.

I'm interested in the correct syntax to use when adding multiple classes to a react element. I want to add styling via an imported module, as well as adding style based on state.

Here is my code.

<div className={[css.mainNavigation, `${this.state.isActive === true ? 'active' : ''}`].join(' ')}>
 </div>

The code is adding .active to my element, however the styling for .active is not coming through.

In my navbar.module.scss, the following code are the relevant lines.

@media (max-width: 650px) {
      .mainNavigation {
        display: none;
      }
      .active {
        display: flex;
      }
    }

I'm guessing it has something to do with the fact that on the frontend, once all the code is rendered, the imported scss style module is using different className's than I am setting. For instance, where I use the classname <div className={css.mainNavigation}>, on the front end, it looks like this:

<div class="_1jGJsP2ItGoSSFd9cLCFqV ">

And after clicking, like this

<div class="_1jGJsP2ItGoSSFd9cLCFqV active">

Yet the .active css, within my modular.scss file is not coming through.

2 Answers 2

1

While you could do this manually I recommend you try clsx https://github.com/lukeed/clsx It is a very small utility designed to do exactly what you want and it's quite easy to use as well.

You can also go for classnames, which more popular https://github.com/JedWatson/classnames

4
  • Is it so complex to do it manually that a small utility is required? I'm learning React at the moment so would prefer doing things manually while in the learning stage. My code above gives the element the correct class, however the styles don't apply. Commented Mar 30, 2020 at 17:24
  • Your code looks good but maybe you didn't import the styles correctly. How are you doing the import of css? And no, of course you don't need it absolutely but it's quite easy to use/learns and helps you a lot Commented Mar 30, 2020 at 17:27
  • Hey, thanks. The styles are both loading from the same module.css sheet, so it should work. At the bottom of my css style sheet, I have this: @media (max-width: 650px) { .mainNavigation { display: none; } .active { display: flex; } } The class is toggling on and off the element, but the menu is not changing display nor registering .active styles. I'm guessing it has something to do with the fact that it looks as though className's are transformed when imported, so maybe it's not getting .active. Commented Mar 30, 2020 at 17:34
  • Hey, I've added to the original question, as it's hard to format properly in comments Commented Mar 30, 2020 at 17:42
0

I figured it out.

I needed to add the className as css.active not active.

${this.state.isActive === true ? css.active : ''}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.