Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
How can I rewrite this conditional css using classnames?

In react component, I'm using css module and I got this conditional css that is working fine, but I would like to refactor it using classnames library.

className = { `${active ? styles.activeLabel : styles.notActiveLabel} 
${weight === 'bold' ? styles.bold : ''}`}

So I tried this but I get error msg saying active will always return false.

className={classnames({styles.activeLabel: !!active}, {styles.bold: weight === 'bold'})}

I also tried styles.activeLabel: active === true, but I get another error message. Basically I want when active prop is true then apply activeLabel class, if active is false then apply notActiveLabel class. How can I accomplish this using classnames?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The Keys on JS Object must to be a string or a number, you can't set a key like:

// this does not work
const object = { key: 'value' }
const object2 = { object.key: true }

So, when you need to get a key with dynamic name you have to do with square brackets

// this works
const object = { key: 'value' }
const object2 = { [object.key]: true }

Try to do something like:

className={classnames({ [styles.activeLabel]: !!active}, {[styles.bold]: weight === 'bold'})}

References:
Multiple classNames with CSS Modules and React
How do I create a dynamic key to be added to a JavaScript object variable

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda