# bind-em-all

> Bind methods to given context

Latest version **1.0.0** (published 2018-02-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install bind-em-all
pnpm add bind-em-all
yarn add bind-em-all
bun add bind-em-all
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-02-12 |
| First published | 2018-02-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | tlareg |
| Maintainers | tlareg |
| Keywords | bind, context, react |

## Links

- npm: https://www.npmjs.com/package/bind-em-all
- Repository: https://github.com/tlareg/bind-em-all
- Homepage: https://github.com/tlareg/bind-em-all#README
- Issues: https://github.com/tlareg/bind-em-all/issues
- npm.io page: https://npm.io/package/bind-em-all

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

- 1.0.0 (latest) — 2018-02-12

## README

<div align="center">
  :hammer::wave:
  <h1>Bind 'Em All</h1>
  <p>Bind methods to given context.</p>
</div>


## Install

```
$ npm install --save bind-em-all
```
or
```
$ yarn add bind-em-all
```

## Usage

### Example 1

```js
import bindEmAll from 'bind-em-all'

class Duck {
  constructor() {
    this.sound = 'QUAAAAACK!!!'

    bindEmAll(this, `
      makeSound
    `)
  }

  makeSound() {
    console.log(this.sound)
  }
}

const donald = new Duck()

// BEFORE
// setTimeout(donald.makeSound.bind(donald), 1000)

// AFTER
setTimeout(donald.makeSound, 1000)

//=>  'QUAAAAACK!!!'
```

### Example 2

```js
import React, { Component } from 'react'
import bindEmAll from 'bind-em-all'

class MyComponent extends Component {
  constructor() {
    // BEFORE

    // this.handleClick1 = this.handleClick1.bind(this)
    // this.handleClick2 = this.handleClick2.bind(this)
    // this.handleClick3 = this.handleClick3.bind(this)
    // this.handleEvent42 = this.handleEvent42.bind(this)
    // this.handleOrder69 = this.handleOrder69.bind(this)

    // AFTER

    bindEmAll(this, `
      handleClick1
      handleClick2
      handleClick3
      handleEvent42
      handleOrder69
    `)
  }

  handleClick1()  { /* using this ... */ }
  handleClick2()  { /* using this ... */ }
  handleClick3()  { /* using this ... */ }
  handleEvent42() { /* using this ... */ }
  handleOrder69() { /* using this ... */ }

  render() {
    return (
      <div>
        <button onClick={this.handleClick1}>CLICK!</button>
        { /* a lot of code using rest of event handlers */ }
      </div>
    )
  }
}
```

## API

```js
/**
 * Binds methods to given context.
 *
 * @param {Object} context - The context that will be bound to given methods.
 * @param {string[]|string} methods - The names of given context methods,
 * that will be bound with context.
 *
 * @returns {Object} context
 */
bindEmAll(context, methods)
```

## License

MIT © tlareg

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