0

Assuming I have a button, I have searched all of the web, .click() function does not work for me, I really do need to call the button click just after I press the button so it will click itself once more (double-click). Thanks for the help!

<button  id='Button1' className="btn btn-primary btn-block btn-signin" onClick={() => {... // the magic code goes here}}
MAP Inc.
  • 17
  • 1
  • 8

1 Answers1

0

Have you considered instead that you could wrap your "onClick" function and call it twice? This seems much cleaner and easier.

For example:

<button id='Button1' className="btn btn-primary btn-block btn-signin" onClick={() => doubleClick()}>

function click() {
  // normal code goes here
}

function doubleClick() {
  click();
  click();
}
Wyatt Arent
  • 588
  • 3
  • 9
  • I need to execute onClick event twice, not the code inside it in order to firstly submit my form and get response from api server and then quickly fire up the second onClick event – MAP Inc. Apr 20 '18 at 21:39
  • 2
    @MAPInc. For what it's worth, what you're describing is definitely an antipattern. You should be calling the function in a promise after your server responds, not forcing a click event. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise – Wyatt Arent Apr 20 '18 at 21:41
  • I can't do so since I have some kind of protected route which can't be accessed outside of render (it is actually wrapped so the authorization works so), the double-click is a wonderful solution there – MAP Inc. Apr 20 '18 at 21:53
  • I do need to refactor my code, I guess I have no solution for this – MAP Inc. Apr 20 '18 at 21:57