简体   繁体   中英

Adding two kebab case css class in an element in react

import React from 'react';
import styles from './stylesheet.moudle.css'

<div className={styles['first-style'] styles['second-style']}>
some content
</div>

How do I add styles['second-style'] into the className? I tried comma and it does not work.

Solved it with:

<div className={`${styles['first-style']} ${styles['second-style']}`}>

I prefer using classnames npm module https://www.npmjs.com/package/classnames

You can do more complex things with this in a clean way.

import classNames from 'classNames'

const condition = true;

<div className={
  classNames(
     styles.first-style,
     styles.second-style,
     {'styles.some-other-conditional-classname': condition})}>

You can also use clsx .

const Toolbar = ({
  className,
  ...rest
}) => {
    <Component
      className={clsx(classes.root, className)}
      {...rest}
    >
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM