Skip to content

domarmstrong/autobind-decorator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

autobind decorator

A class or method decorator which binds methods to the instance so this is always correct, even when the method is detached.

This is particularly useful for situations like React components, where you often pass methods as event handlers and would otherwise need to .bind(this).

Before:
<button onClick={ this.handleClick.bind(this) }></button>

After:
<button onClick={ this.handleClick }></button>

As decorators are a part of future ES7 standard they can only be used with transpilers such as Babel.

Installation:

% npm install autobind-decorator

Example:

import autobind from 'autobind-decorator'

class Component {

  constructor(value) {
    this.value = value
  }

  @autobind
  method() {
    return this.value
  }
}

let component = new Component(42)
let method = component.method // .bind(component) isn't needed!
method() // returns 42


// Also usable on the class to bind all methods

@autobind
class Component { }

About

Decorator for binding method to an object

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 87.3%
  • Makefile 12.7%