How to use clsx in React

后端 未结 3 1939
迷失自我
迷失自我 2021-01-31 08:50

I am trying to understand some uses of clsx in assigning classnames to a component in React.

The construct

className={clsx(classes.menuButton, open &         


        
3条回答
  •  执笔经年
    2021-01-31 09:26

    Many people already explained it pretty well. I still wanted to add an example containing className. In the example you can see different classes, applied if a given condition evaluates to true. In the example you can apply a class with a boolean value, a boolean variable or a compared string (If match, return true). The class classes.drawer is always applied and does not depend on a condition.

    className={clsx(classes.drawer, {                  // classes.drawer is applied always
              [classes.drawerOpen]: true,              // classes.drawerOpen is applied always, bool = true
              [classes.drawerClose]: !open,            // you can also use boolean variable
              [classes.drawerRed]: colorVar === 'red', // you can also use string variable
            })}
    

提交回复
热议问题