react-bind-handlers

1.0.5 • Public • Published

Build Status

react-bind-handlers

Class decorator which binds class methods to instance, so this always refers to an instance

Allows to avoid repetitive bind(this) in React component constructor:

class Component extends React.Component {
    constructor() {
        this.handleOpenBtnTap = this.handleOpenBtnTap.bind(this);
        this.handleCloseBtnTap = this.handleCloseBtnTap.bind(this);
        this.handleMenuItemTap = this.handleMenuItemTap.bind(this);
        ...
    }
}

becomes:

class Component extends React.Component {
    ...
}

export default bindHandlers(Component);

By default all methods with handle* prefix will autobind to this

How to use:

Install:

npm i react-bind-handlers

Import and use:

import { bindHandlers } from 'react-bind-handlers';
class Component extends React.Component {}
export default bindHandlers(Component);
import { bind } from 'react-bind-handlers';
class Component extends React.Component {}
export default bind(regExp)(Component);

Package Sidebar

Install

npm i react-bind-handlers

Weekly Downloads

3

Version

1.0.5

License

ISC

Last publish

Collaborators

  • fiolkaf