Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda