Giala Jefferson
Giala Jefferson

Reputation: 1929

if else condition using classNames

There's a package called classNames. I try to use it to hide show when a user switch tab, can I pass in the expression like this instead of using a flag checking true or false?

<div className={classNames("tab", {(this.state.selectedTab === 2), "hide"})}>
//content
</div>

But above code won't work.

Upvotes: 2

Views: 3553

Answers (1)

Mayank Shukla
Mayank Shukla

Reputation: 104379

I think, you can use it like this:

className={classNames("tab", {"hide": this.state.selectedTab === 2})}

Reason is, key will be the the name of class and in place of value you can use any condition which will return true or false, if condition will be true then hide will be applied otherwise it will get ignored.

Upvotes: 2

Related Questions