ES6 Template Literals
<div className={`${styles.class1} ${styles.class2}`}></div> <div className={`${class1} anotherClass ${class1}`}></div> <li className={`li active`}>Stacy</li> <li className={`li ${this.state.isActive ? 'active' : ''}`}>Stacy<li>
Pass Comma Separated Array
<Link className={[classes.button, classes.buttonFirst]}> <Link className={['first', 'second']}>
Using classnames Library
Another way is to use classnames library and with it, you can also toggle classes depending on the state of your component.
# via npm npm install classnames # or Yarn (note that it will automatically save the package to your `dependencies` in `package.json`) yarn add classnames
var classNames = require('classnames'); classNames('foo', 'bar'); // => 'foo bar'
let buttonType = 'primary'; classNames({ [`btn-${buttonType}`]: true });
Leave a reply