Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use clsx in React

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

The construct

className={clsx(classes.menuButton, open && classes.hide)} 

is clear enough. It applies 'classes.menuButton', and also applies 'classes.hide' if the value of the boolean 'open' is true.

My question relates to this second example:

className={clsx(classes.appBar, {[classes.appBarShift]: open })}

This will apply 'classes.appBar'. But what is the meaning of the second parameter?

like image 738
millport Avatar asked Aug 19 '19 12:08

millport


People also ask

What is CLSX in Reactjs?

clsx is generally used to conditionally apply a given className. This syntax means that some class will only be applied if a given condition evaluates to true const menuStyle = clsx({ [classes. root] : true, //always applies [classes.

Why we use CLSX in react?

the clsx() Function in React clsx() function from the package of the same name is a JavaScript utility used to set up conditions for setting the value of the className attribute. It accepts an unlimited number of arguments not limited to one specific type.

What is CLSX material UI?

clsx is a tiny utility for constructing className strings conditionally, out of an object with keys being the class strings, and values being booleans.

What is CLSX package?

A tiny (228B) utility for constructing className strings conditionally. Also serves as a faster & smaller drop-in replacement for the classnames module. This module is available in three formats: ES Module: dist/clsx.


Video Answer


3 Answers

clsx is generally used to conditionally apply a given className

This syntax means that some class will only be applied if a given condition evaluates to true

const menuStyle = clsx({
    [classes.root] : true, //always applies
    [classes.menuOpen] : open //only when open === true
})

In this example [classes.menuOpen] (which will evaluate to something like randomclassName123) will only be applied if open === true


clsx basically outputs a string interpolation. So you don't have to necessarily use it.

There are many supported syntax that you can check in the official docs

Instead of

<div className={`${classes.foo} ${classes.bar} ${classes.baz}`} />

You can use it like this

const { foo, bar, baz } = classes
const style = clsx(foo, bar, baz)

return <div className={style} />
like image 133
Dupocas Avatar answered Oct 19 '22 01:10

Dupocas


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
        })}
like image 28
Amel Avatar answered Oct 19 '22 02:10

Amel


classes.appBarShift will be applied only if open evaluates to true. If the array has more classes all of them will be applied if open evaluates to true

like image 3
Aditya Avatar answered Oct 19 '22 02:10

Aditya