220

I know in vanilla JavaScript, we can do:

onclick="f1();f2()"

What would be the equivalent for making two function calls onClick in ReactJS?

I know calling one function is like this:

onClick={f1}
2240
  • 1,547
  • 2
  • 12
  • 30
user2554585
  • 3,489
  • 6
  • 20
  • 23
  • 1
    Very simple: pass a function that calls the two functions, just like you would to with `ele.onclick = ...` or `addEventListener`. – Felix Kling Sep 26 '14 at 22:44
  • In case your 1st function is setState and you need to make sure this is happening/taken into account before the 2nd function runs, you will find this post quite helpful: https://medium.com/better-programming/when-to-use-callback-function-of-setstate-in-react-37fff67e5a6c – Nasia Makrygianni Oct 14 '19 at 14:27

11 Answers11

357

Wrap your two+ function calls in another function/method. Here are a couple variants of that idea:

1) Separate method

var Test = React.createClass({
   onClick: function(event){
      func1();
      func2();
   },
   render: function(){
      return (
         <a href="#" onClick={this.onClick}>Test Link</a>
      );
   }
});

or with ES6 classes:

class Test extends React.Component {
   onClick(event) {
      func1();
      func2();
   }
   render() {
      return (
         <a href="#" onClick={this.onClick}>Test Link</a>
      );
   }
}

2) Inline

<a href="#" onClick={function(event){ func1(); func2()}}>Test Link</a>

or ES6 equivalent:

<a href="#" onClick={() => { func1(); func2();}}>Test Link</a>
Mo.
  • 26,306
  • 36
  • 159
  • 225
Rob M.
  • 35,491
  • 6
  • 51
  • 50
31

Maybe you can use arrow function (ES6+) or the simple old function declaration.

Normal function declaration type (Not ES6+):

<link href="#" onClick={function(event){ func1(event); func2();}}>Trigger here</link>

Anonymous function or arrow function type (ES6+)

<link href="#" onClick={(event) => { func1(event); func2();}}>Trigger here</link>

The second one is the shortest road that I know. Hope it helps you!

19

React Functional components

             <button
                onClick={() => {
                  cancelOrder();
                  handlerModal();
                }}
             >
                Cancel
             </button>
Ivan K.
  • 748
  • 7
  • 11
16

Calling multiple functions on onClick for any element, you can create a wrapper function, something like this.

wrapperFunction = () => {
    //do something
    function 1();
    //do something
    function 2();
    //do something
    function 3();
}

These functions can be defined as a method on the parent class and then called from the wrapper function.

You may have the main element which will cause the onChange like this,

<a href='#' onClick={this.wrapperFunction}>Some Link</a>
Ashish Singh
  • 744
  • 6
  • 15
9

Best Way:

onClick={ () => { f1(); f2();} }
Aun Shahbaz Awan
  • 559
  • 7
  • 10
4

If you have to apply two function on a button click at the same time, then we can simply add like this in React js.

<button onClick={()=> {fun1(); fun2()}}>Show more</button>
2

You can simply wrapped it inside []. This will worked as well in material UI button.

<Button onClick={(event) => [function1(), function2()]}>Click Me</Button>

Click to see sample code

Click to see the output

2

Let say you have a button and you want to execute two onClick events, to show the hidden text and hide the button with just 1 click.

let a = 'invisible'
  let b = 'visible'
  const [show, setShow] = useState(a)
  const [buttonshow, setButtonShow] = useState(b)

<button onClick={() => {setButtonShow(a); setShow(b)}}>Read More</button>
<p>This text will show after you press the button Read more and Read
more will be hidden from the DOM
</p>

as you see, the anonymous function just returned two functions.

{setButtonShow(a); setShow(b)}
Dharman
  • 30,962
  • 25
  • 85
  • 135
saran
  • 356
  • 1
  • 3
  • 14
2

//Don't do this way
function App() {
  return (
    <div>
      <button
        onClick={() => {
          console.log('hello');
          console.log('world');
        }}>
        I'm a button
      </button>
    </div>
  );
}

//Do this way
function App() {

  function fun1() {
    console.log('hello');
  }

  function fun2() {
    console.log('world');
  }

  return (
    <div>
      <button onClick={() => {fun1();fun2();}}>Click</button>
    </div>
  );
}
Ovi
  • 179
  • 1
  • 4
0

You can use nested.

There are tow function one is openTab() and another is closeMobileMenue(), Firstly we call openTab() and call another function inside closeMobileMenue().

function openTab() {
    window.open('https://play.google.com/store/apps/details?id=com.drishya');
    closeMobileMenue()   //After open new tab, Nav Menue will close.  
}

onClick={openTab}
Ankit Kumar Rajpoot
  • 5,188
  • 2
  • 38
  • 32
0

A simple one-liner:

onClick={ () => f1() ||  f2() } 
Jay Joshi
  • 1,402
  • 1
  • 13
  • 32