Introduction
In the previous article, we learned about the destructuring of props and states and the basics of event handling in React. In this article, we will learn about binding event handlers the number of ways binding can be done, and also methods that can be used as props.
Binding Event Handlers
In the previous article, we reviewed what an Event handler is in React. Now in this article, we will go into detail about how to bind event handlers with React. React provides 4 ways to bind the event handlers.
Binding in Render() method
In React, we can attach events using the bind method in a render function. To call the bind method in the render function, we will use .bind(this) in render() function.This is an ES6 class, a plain JavaScript function, therefore, it inherits bind() from function prototype. When using any event inside JSX, this keyword will point to the class instance to which it is called. Using this method may cause performance issues as the function is reallocated on every render. The performance issues may not be visible in small React applications but it may cost very much in bigger React applications.
For example
import React, { Component } from 'react';
class EventBinding extends Component {
constructor(props) {
super(props);
this.state = {
message: "Testing"
};
}
clickHandler() {
this.setState({
message: "Tested"
});
}
render() {
return (
<p>
{this.state.message}
<button onClick={this.clickHandler.bind(this)}>Test</button>
</p>
);
}
}
export default EventBinding;
Now, export the EventBinding in App.js.
import React, { Component } from "react";
import "./App.css";
import logo from "./logo.svg";
import EventBinding from "./Components/EventBinding";
class App extends Component {
render() {
return (
<p className="App">
<img src={logo} className="App-logo" alt="logo" />
<EventBinding></EventBinding>
</p>
);
}
}
export default App;
Now, the output will be displayed as below.

At the click of a button, the text is changed.

Arrow function body
This approach uses an arrow function body in the render method
Update the code as shown below.
<button onClick={() => this.clickHandler()}>Test</button>
This will display the output the same as above.
Binding in Constructor
In React, we have another way to bind events, i.e., using binding in the constructor, as shown below.
As per the above example, we will define the event binding in the constructor.
import React from 'react';
class EventBinding extends React.Component {
constructor(props) {
super(props);
this.state = {
message: "Testing"
};
this.clickHandler = this.clickHandler.bind(this);
}
clickHandler() {
this.setState({
message: "Tested"
});
}
render() {
return (
<p>{this.state.message}<br/>
<button onClick={this.clickHandler}>Test</button>
</p>
);
}
}
export default EventBinding;
The output will be the same as shown below.


Using this approach will bind the event once in the constructor so it is a better approach than the above 2 and it doesn't rerender on every call.
Class property approach
This approach is an experimental feature. This approach uses the class property as an arrow function. Using class properties, you can simplify your handler functions. This will create a new function on every render.
For example
import React from 'react';
class EventBinding extends React.Component {
constructor(props) {
super(props);
this.state = {
message: "Testing"
};
}
clickHandler = () => {
this.setState({
message: "Tested"
});
};
render() {
return (
<p>
{this.state.message}<br/>
<button onClick={this.clickHandler}>Test</button>
</p>
);
}
}
export default EventBinding;
This will also display the same output as displayed above.
Higher Order Component (HOC)
A Higher Order Component is an advanced technique in React for reusing component logic in our applications. HOCs are a pattern that emerged from the React Composition nature. The HOC is not a part of React API.








Vikram BhardwajPosted Jan 31, 2022, 3:47 AM
Thank you Priyanka for awesome React series sharing small correction for all readers import React from 'react'; class ChildComponent extends React.Component { render() { return( <div><button onClick={()=>this.props.greetChild('child')}>Call Parent Component</button></div> ) } } export default ChildComponent;
SisayPosted Nov 30, 2019, 2:57 PM
You need to update the ChildComponent as well for the greetChild props you are passing otherwise it doesn't work when you click on second button.
Dnyaneshwar BabarPosted Sep 11, 2019, 11:56 PM
In Arrow function body needs below correction. Old <button onClick={() => this.clickHandler}>Test</button> Changed <button onClick={() => this.clickHandler()}>Test</button>