0.1.4 • Published 8 years ago

class-autobind v0.1.4

Weekly downloads
34,686
License
ISC
Repository
github
Last release
8 years ago

Method auto-bind for ES6 (ES2015) classes

This package provides a single function, autobind, for use within a constructor to bind all methods to the instance itself.

For example, this allows us to pass a method to an event handler element.addEventListener('click', this.onClick) and be sure the onClick method will always be called with the right context.

Note: This has some specific logic for React, but could be used in any project.

Usage:

import autobind from 'class-autobind';

class MyComponent extends React.Component {
  constructor() {
    super(...arguments);
    autobind(this);
  }
  render() {
    return <button onClick={this.onClick}>Click Me</button>;
  }
  onClick() {
    console.log('Button Clicked');
  }
}

Advanced Usage:

If your component will possibly be subclassed (you really should not do this, but some third-party libraries like react-css-modules do so) then you will need to specify which prototype will be the source of methods that are to be automatically bound.

import autobind from 'class-autobind';

class MyComponent extends React.Component {
  constructor() {
    super(...arguments);
    autobind(this, MyComponent.prototype); // Note the second parameter.
  }
  render() {
    /* ... */
  }
}

class MySubClassedComponent extends MyComponent {
  /* This is probably a very bad idea. */
}

License

This software is BSD Licensed.

react-img-carousel-x@idkman/gold-material-tablereact-rte-i18n@everything-registry/sub-chunk-1332geta-checkoutgillespie59-react-rtegraphql-guru-jsondbgraphql-guru-mongodbgraphql-guru-redisgraphql-guru-rethinkdbgraphql-guru-sequelizeelm-react-reduxds-react-rtefetch-hoc-reduxhtml-verify-react-rtehf-react-rteless.js@psychwire/react-rteinterwebjsll-react-rtejx-react-rtemuse-rtenode-bits-internal-databasenode-bits-passwordnode-bits-restmtarapata-react-rtemeteor-compounder-coreqbl-react-rtematerial-ui-form-helpersreact-draft-rtereact-fetch-loadingreact-native-qiscus-sdkreact-richtextreact-recurly-modalreact-otpreact-rte-semanticreact-rte-with-cloudinaryreact-rte-ytreact-rte2react-rte-18supportreact-rte-browserifyreact-rte-browserify-imagereact-rte-es5react-rte-gcreact-rte-imagereact-rte-imagesupportreact-rte-link-extendedreact-rte-marked-unsafereact-rte-materialreact-rte-mentionreact-rte-modifiedreact-rtereact-rte-17react-rte-qbreact-rte-ref-fixreact-rte-scoped-stylereact-stripe-modalreact-band-walletreact-alex-rteroy-gbiv_react-rtern-zoomableswift-react-rtetrespass-visualizationstimecamp-v2-javascript-clientsemantic-ui-react-rtesur41-react-rtesshttpshengnian-editorvisual-testing@graphql-guru/ide@humanz/react-rte@zalastax/nolb-clasaejis-react-rte@trojanowski/react-rteandersryanc--react-rte@4nduril/react-rte@agolo/output-config-rteband-walletcw-print-servicecw-printer-service@happens-lol/react-rte@ikonintegration/react-rtedraft-rtedraftjs-evanddexmosdealroom-react-rte@matthewberryman/react-rte@occmundial/occ-atomicbuzhuo-k8s-uicl-react-rte@cosmology/cli@cosmology/core@davidsackett/react-rte@dock365/react-rte
0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago