I
I
Ismail2020-05-07 20:06:51
Sass
Ismail, 2020-05-07 20:06:51

Is it possible to make className dynamic in React?

Good time of the day.
I am working with scss modules and want to dynamically change classes like this:

import classes from './ItemForm.module.scss';
...
<div className={`${classes}.${itemStatus}`}>
     {status}
</div>


Ie itemStatus comes to me, it is different, and there are different classes for it in the scss module. I don't want to make a huge if else construct, is it possible somehow to nest itemStatus dynamically in className?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
mike25, 2020-05-08
@gosugod

used the classnames library, see if it helps

A
abberati, 2020-05-07
@abberati

It's just an object
classes[“foo”]
classes[itemStatus]

V
Vladimir, 2020-05-08
@HistoryART

import classes from './ItemForm.module.scss';
const myClasses = [

   {id: 1, className: 'myClass'},
   {id: 2, className: 'myClass2'}

]
<div className={myClasses.map(el => `${el.className} `)}>
     {status}
</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question