Skip to content

fiolkaf/react-bind-handlers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);

About

Bind react component handlers with "this" context

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published