Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!