NĂŁo sou um expert em React mas tenho trabalhado com o framework nos Ășltimos meses e posso dizer que o pacote classnames Ă© algo que tem ajudado bastante.
Como cĂłdigo Ă© melhor que palavras, vamos ao que interessa. Eu costumava a fazer algo assim:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MyComponent = ({ condition }) => ( | |
<div className={`myClass ${condition ? 'is-active' : ''}`}></div> | |
) |
ouâŠ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MyComponent = ({ condition }) => { | |
const myComponentClass = condition ? 'is-active' : ''; | |
return ( | |
<div className={`myClass ${myComponentClass}`}></div> | |
) | |
} |
JĂĄ funciona! :) Mas dĂĄ pra deixar mais legal, principalmente pensando quando o nĂșmero de classes CSS aumentar. Foi entĂŁo que apareceu essa maravilha chamada classnames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import classnames from 'classnames'; | |
const MyComponent = ({ condition, anotherCondition }) => { | |
const myComponentClasses = classNames({ | |
'myClass': true, | |
'is-active': condition, | |
'has-icon': anotherCondition | |
}); | |
return ( | |
<div className={myComponentClasses}></div> | |
) | |
} |