Home / Snippets / How to Bind Event Handlers in React JS Component by Avoiding Constructor Declaration
How to Bind Event Handlers in React JS Component by Avoiding Constructor Declaration cover

How to Bind Event Handlers in React JS Component by Avoiding Constructor Declaration

224

2 years ago

0 comments

To bind event handlers in React JS component you can easily define arrow functions, by making use of this method you will be accessing the same context in the component. 
import React from 'react';

class Toggle extends React.Component {
  state = { name: 'Hello dad' };

  render() {
    return (
      <button onClick={ this._handleButtonClick }>
        Greet Dad
      </button>
    );
  }

  _handleButtonClick = () => {
    console.log(`Message: ${ this.state.name }`);
  }
}
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this