I'm using the following code to dynamically set a className in a React component based upon a boolean from props:
<div className={this.props.menuOpen ? 'inactive' : 'active'}>
...
</div>
However, I'm also using CSS Modules, so now I need to set the className to:
import styles from './styles.css';
<div className={styles.sideMenu}>
...
</div>
I'm having trouble with this - I tried using classnames to gain more control with multiple classes, but because I need the end result to be that the className is set to both styles.sideMenu
AND styles.active
(in order for CSS Modules to kick in) I'm unsure how to handle this.
Any guidance is greatly appreciated.
classnames
work for you? This is the case it's designed for. – Majorsclassnames
:let classNames = classnames(styles.sideMenu, { active: this.props.menuOpen, });
However, the keyactive
must bestyles.active
(so that I can place my css in the imported stylesheet), and setting to this creates an error. Perhaps I'm misunderstanding the documentation? – Trickster