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

270
Vistas
How to inject JavaScript inside JSX attribute's string?

I'm trying to put some default props inside className=" px-6 py-2 bg-black rounded-full text-black " and when I use this component elsewhere I will be passing background or text color as props.

What's the right syntax for this manipulation?

I was trying to use grave accent ` with classic ${} but it doesn't work out in my react application.

<button className=`btn px-6 py-2 bg-${} rounded-full`>
  shop
</button>

enter image description here

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

0

You'd use a JSX expression, which is {expression} (in a JSX context). So you might well use a template literal, within the {}, like this (assuming backgroundColor and textColor contain the props):

return (
    <Button className={`px-6 py-2 bg-${backgroundColor} text-${textColor}`}>
        ...
    </Button>
);

It doesn't have to be a template literal, it could be a string concatenation expression:

return (
    <Button className={"px-6 py-2 bg-" + backgroundColor + " text-" + textColor}>
        ...
    </Button>
);

Or to keep the JSX from getting to hard to work with, do it prior to the JSX:

const theClass = `px-6 py-2 bg-${backgroundColor} text-${textColor}`;
return (
    <Button className={theClass}>
        ...
    </Button>
);
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