Calling a Function in React

Google Images

Difference between calling a function in React
  1. onClick={this.myFunc}, 
  2. onClick={() => this.myFunc()}
  3. onClick={this.myFunc.bind(this}}

 here is a guide when to use which one.


onClick={this.myFunc}:

we can call the functions in react.js like this way but there is not need to place small braces after this.myFunc but the drawback of this way calling a function is we can't pass a parameters because we can't use small braces .

onClick={() => this.myFunc()}:

In this way of calling a function is helpful because we can pass a parameters (e.g onClick={() => this.myFunc(x, y)} ) if we want else we can leave it like ( onClick={() => this.myFunc()} ). 


onClick={this.myFunc.bind(this}}:

In this way we can call the function directly in order to bind the state with the function in order to call it

Post a Comment

0 Comments