Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React onClick setState then run function

Good day!

Is it possible to trigger multiple functions on the onClick event ?

I would like when the user clicks on my button to setState and then run a function. I managed to update my state but I am not sure how to proceed to add the second step which would be to run my function called "goToOrderTemplate()"

<button
  type="button"
  className="btn btn-primary"
  style={{ width: 250 }}
  onClick={() =>
    this.setState({
      auth: this.passwordValidation()
    })
  }
>

Any help woul dbe greatly appreciated!

like image 424
Kev Avatar asked Sep 14 '25 15:09

Kev


1 Answers

setState takes a second parameter, which is a callback that gets called back when the state was set. You should use that if goToOrderTemplate depends on the state being set:

this.setState({
  auth: this.passwordValidation(),
}, goToOrderTemplate)

In the general case that you want to call two functions, just do that:

onClick={() => { 
  this.setState({
    auth: this.passwordValidation(),
  });
  goToOrderTemplate();
}}
like image 180
Jonas Wilms Avatar answered Sep 17 '25 06:09

Jonas Wilms