Open In App

ReactJS | Binding Event Handlers

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:




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().






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.

Error




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;




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;




this.clickHandler = this.clickHandler.bind(this)




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


Article Tags :