# class-autobind

> Method auto-bind for ES6 (ES2015) classes

Latest version **0.1.4** (published 2016-06-13) · ISC license · 0 weekly downloads

## Install

```sh
npm install class-autobind
pnpm add class-autobind
yarn add class-autobind
bun add class-autobind
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.4 |
| Published | 2016-06-13 |
| First published | 2016-03-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 25 |
| Author | team@kodefox.com |
| Maintainers | sstur |
| Keywords | react, autobind, es6-class |

## Links

- npm: https://www.npmjs.com/package/class-autobind
- Repository: https://github.com/kodefox/class-autobind
- Homepage: https://github.com/kodefox/class-autobind#readme
- Issues: https://github.com/kodefox/class-autobind/issues
- npm.io page: https://npm.io/package/class-autobind

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.4 (latest) — 2016-06-13
- 0.1.3 — 2016-06-08
- 0.1.2 — 2016-04-25
- 0.1.1 — 2016-03-21
- 0.1.0 — 2016-03-14

## README

# 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:

```js
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](https://npmjs.com/package/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.

```js
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](/LICENSE).

---
_Source: https://npm.io/package/class-autobind · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
