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

140
Vistas
addClass only once onClick ReactJS

Hi I'm new to ReactJS so I'm importing some work of mine to my project and turning it into components. I have a span acting as a button with keyframe animations, but when i load the page it gets animated. One solution i have is to make a .class.animation and give the .animation class on the first click. The thing is that I know how to code it in jquery but not on react.

The idea is to convert this into react:

$('.navTrigger').onClick(function () {
    $(this).addClass('animations');
});

Right now I have this:

const [isActive, setActive] = useState("false");
    
const handleToggle = () => {
    setActive(!isActive);
};

return (
    <span onClick={handleToggle} className={`navTrigger ${isActive ? "" : "animations"} ${isActive ? "" : "active"}`}>
          <i></i>
          <i></i>
          <i></i>
     </span>
);

I have a function where i toggle the active class to run backward and forward animations.

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

0

What the code below does is on web load, the span starts with navTrigger class. The first time you click it its's given animations and active class. And then if you interact with it again it takes out active class and leaves animations there

function MenuButton() {

    const [isActive, setActive] = useState(false);
    const [animating, setAnimating] = useState(false);
 
    const handleToggle = () => {
        setActive(!isActive);
        setAnimating(true);
    };


    return (
        <span onClick={handleToggle} className={`navTrigger ${animating ? "animations" : ""} ${isActive ? "active" : ""}`}>
            <i></i>
            <i></i>
            <i></i>
        </span>
    );
} 

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