ReactJS | Binding Event Handlers Last Updated : 11 Nov, 2022 Comments Improve Suggest changes Like Article Like Report In ReactJS, we need to bind events so that the this keyword would not return an "undefined". In this article, we are going to see the different ways in which we can bind event handlers in ReactJS. First, let's make a new class component and name it Binding.js. Make a simple class component with a simple message state and render a simple button as shown below. Don't forget to import this component in App.js file. Binding.js: JavaScript import React, { Component } from 'react'; class EventBind extends Component { constructor(props) { super(props) this.state = { message: 'Welcome' } } render() { return ( <div> <h3>{this.state.message}</h3> <button>Click</button> </div> ) } } export default EventBind; Let's write an event that changes the state of message from 'Welcome' to 'Farewell' whenever the button is clicked. So let's define an onClick method to the button and write an event handler clickHandler(). JavaScript import React, { Component } from 'react'; class EventBind extends Component { constructor(props) { super(props) this.state = { message: 'Welcome' } } clickHandler() { this.setState({ message:'Farewell' }) } render() { return ( <div> <h3>{this.state.message}</h3> <button onClick={this.clickHandler}>Click</button> </div> ) } } export default EventBind; Output: Now if we run the application and click on the button, we get an error. This is because this returns an "undefined". This is why we need to bind our events. ErrorBinding Event Handler in Render Method: We can bind the handler when it is called in the render method using bind() method. JavaScript import React, { Component } from 'react'; class EventBind extends Component { constructor(props) { super(props) this.state = { message: 'Welcome' } } clickHandler() { this.setState({ message:'Farewell' }) } render() { return ( <div> <h3>{this.state.message}</h3> <button onClick={this.clickHandler.bind(this)}> Click</button> </div> ) } } export default EventBind; Binding Event Handler using Arrow Function: This is pretty much the same approach as above, however, in this approach we are binding the event handler implicitly. This approach is the best if you want to pass parameters to your event. JavaScript import React, { Component } from 'react'; class EventBind extends Component { constructor(props) { super(props) this.state = { message: 'Welcome' } } clickHandler() { this.setState({ message:'Farewell' }) } render() { return ( <div> <h3>{this.state.message}</h3> <button onClick={() => this.clickHandler()}> Click </button> </div> ) } } export default EventBind; Binding Event Handler in the Constructor: In this approach, we are going to bind the event handler inside the constructor. This is also the approach that is mentioned in ReactJS documentation. This has performance benefits as the events aren't binding every time the method is called, as opposed to the previous two approaches. Just add the following line in the constructor for this approach, JavaScript this.clickHandler = this.clickHandler.bind(this) Binding Event Handler using Arrow Function as a Class Property: This is probably the best way to bind the events and still pass in parameters to the event handlers easily. JavaScript import React, { Component } from 'react'; class EventBind extends Component { constructor(props) { super(props) this.state = { message: 'Welcome' } } clickHandler = () => { this.setState({ message:'Farewell' }) } render() { return ( <div> <h3>{this.state.message}</h3> <button onClick={this.clickHandler}> Click </button> </div> ) } } export default EventBind; Output: All of these approaches offer the same solution to the problem, you can use any of them according to your requirements. State after button is clicked Comment More infoAdvertise with us Next Article ReactJS | Binding Event Handlers rahulhegde97 Follow Improve Article Tags : JavaScript Web Technologies Write From Home ReactJS Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon 8 min read JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as 15+ min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read What is DFD(Data Flow Diagram)? Data Flow Diagram is a visual representation of the flow of data within the system. It help to understand the flow of data throughout the system, from input to output, and how it gets transformed along the way. The models enable software engineers, customers, and users to work together effectively d 9 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of 10 min read Like