0

I have two functions but do not have their argument and i just want to run the two functions on a button's onClick method.

I know the following code works on react.

onClick={handleSubmit}

Is it possible to do something like

onClick={handleSubmit; doSomethingElse;}

Inside the onClick method i need to call the Formik's handleSubmit method. Note: I am trying to call the handleSubmit method from a button outside the form.

React Formik bind the external button click with onSubmit function in <Formik>

As explained in this article i can just onClick={handleSubmit} do this to call the function. But i also need to call another function on the same onClick function. How can i call both of them from the same onClick?

Sagar Acharya
  • 1,763
  • 2
  • 19
  • 38

3 Answers3

2

Just create another function and call both your function in the third one.

function auxilaryFunction (e) {
 handleSubmit(e);
 doSomethingElse(e);
}

Then update onClick method like following:-

onClick={auxilaryFunction}

Now it will call both your functions. If you don't want to use e (event) then simply removed it from both of the functions.

aditya81070
  • 678
  • 1
  • 5
  • 22
0
onClick={this.handleClick}

handleClick(){
   function1();
   function2();
}
0

make function like

handleSubmit = () => {
  this.doSomethingElse();
  this.doSomethingElse();

} when click on handleSubmit automatic functions called inside the function

Salman Mehmood
  • 263
  • 2
  • 6