Home > Blockchain >  How do I use an onclick to do two things at once in react?
How do I use an onclick to do two things at once in react?

Time:07-11

I want to use an onClick on an element to do two things once you click on it.

I know that this is how you do one, but how do you do two or more?

<div onClick={myFunction}></div>

I am using React 17 if that makes any difference.

CodePudding user response:

Replace it with an arrow function.

<div onClick={()=>{myFunction();mySecondFunction();}}></div>

CodePudding user response:

You create an alternative 'myFunction' that does the two things that you want. E.g.

function myFunction() {
    thing1();
    thing2();
}

or you put a lambda function in:

onClick={() => {thing1(); thing2()}}
  • Related