# react-auto-bind

> Auto bind methods to their class instance excluding React Lifecycle Methods

Latest version **0.4.2** (published 2017-11-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-auto-bind
pnpm add react-auto-bind
yarn add react-auto-bind
bun add react-auto-bind
```

## 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.4.2 |
| Published | 2017-11-10 |
| First published | 2016-10-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Ritesh Kumar |
| Maintainers | housing |
| Keywords | auto, bind, class, methods, method, automatically, prototype, instance, function, this, self, react-auto-bind |

## Links

- npm: https://www.npmjs.com/package/react-auto-bind
- Repository: https://github.com/housinghq/react-auto-bind
- Homepage: https://github.com/housinghq/react-auto-bind#readme
- Issues: https://github.com/housinghq/react-auto-bind/issues
- npm.io page: https://npm.io/package/react-auto-bind

## Recent versions

- 0.4.2 (latest) — 2017-11-10
- 0.4.1 — 2017-11-10
- 0.4.0 — 2017-07-13
- 0.3.0 — 2017-04-10
- 0.2.0 — 2016-10-10

## README

# react-auto-bind [![Build Status](https://travis-ci.org/housinghq/react-auto-bind.svg?branch=master)](https://travis-ci.org/housinghq/react-auto-bind)

> Automatically bind methods to their class instance. This is optimized for react. It doesn't bind render and other lifecycle methods.


## Install

```
$ npm install --save react-auto-bind
```

## Usage

```js
const autoBind = require('react-auto-bind');

class Unicorn {
	constructor(name) {
		this.name = name;
		autoBind(this);
	}
	message() {
		return `${this.name} is awesome!`;
	}
}

const unicorn = new Unicorn('Rainbow');

// Grab the method off the class instance
const message = unicorn.message;

// Still bound to the class instance
message();
//=> 'Rainbow is awesome!'

// While message() would not be bound & would have resulted in
message();
//=> Error: Cannot read property 'name' of undefined
```

### Bind only some methods

```js
const autoBind = require('react-auto-bind');

class Griffin {
	constructor(name) {
		this.name = name;
		autoBind(this, 'amaze');
	}
	amaze() {
		return `${this.name} is amazing!`;
	}
	message() {
		return `${this.name} is undefined!`;
	}
}

const buckbeak = new Griffin('Buckbeak');

// Grab the method off the class instance
const message = buckbeak.message;
const amaze = buckbeak.amaze;

// Still bound to the class instance
amaze();
//=> 'Buckbeak is amazing!'

// Without `autoBind(this)`, the above would have resulted in
message();
//=> Error: Cannot read property 'name' of undefined
```


## API

### autoBind(self, [...bindOnly])

Bind methods in `self` to their class instance. Returns the `self` object.

#### self

Type: `Object`

Object with methods to bind.

#### bindOnly

Type: `Strings`

If more than one parameters are passed then only those methods are bound.

## Related

- [bind-methods](https://github.com/sindresorhus/bind-methods) - Bind all methods in an object to itself or a specified context
- [auto-bind](https://github.com/sindresorhus/auto-bind) - Automatically bind all methods to their class instance.

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