M
M
MRcracker2021-10-28 13:43:16
React
MRcracker, 2021-10-28 13:43:16

How to correctly set the class?

I use the module.css approach in my work. In order to set a class, I have to write What should I do if I use hooks and I need to specify several classes?
<div className={styles.tabsContentWrap}></div>

<div
          className={
            toggleState === 1 ? "tabsContent active-content" : "tabsContent"
          }
        >
</div>


Write classes only globally or is it possible to somehow write them using css modules?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Aetae, 2021-10-28
@MRcracker

Use classNames .
Without this, there is no life.)

D
deantek, 2021-10-28
@deantek

className={`tabsContent ${toggle === 1 ? 'toggle' : ''}`}

className={`${s.container} ${isMobile ? s.mobile_container : ''}`}>

A
Alexander, 2021-10-28
@aleksand44

styles.tabsContentWrapresolves to a regular string
primitively you can do this

className={toggleState === 1 ? `${styles.tabsContentWrap} ${styles.activeContent}` : styles.tabsContent}

better look at the "classnames" library, it makes it easy to compose classes

F
Frontend developer, 2021-10-28
@markak

https://www.npmjs.com/package/classnames

const className = classNames("tabsContent", {"active-content":  toggleState === 1});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question