@shanedaugherty/bind-methods

Bind methods is a simple helper function used to bind a context to multiple class or prototype methods at once.

Usage no npm install needed!

<script type="module">
  import shanedaughertyBindMethods from 'https://cdn.skypack.dev/@shanedaugherty/bind-methods';
</script>

README

Bind Methods

Bind methods is a simple helper function used to bind a context to multiple class or prototype methods at once.

It was created primarily for use with React Components, but has no dependencies on React.

Parameters

Methods

type: Array

An array of methods names (strings). Each will be used to access the method magically like so: context['method'].

Context

type: Object, *

The context that shall be bound to each method.

Example usage with React

class extends React.Component {
  constructor() {
    super();
    bindMethods(['someFunction', 'anotherFunction'], this);
  }
  
  someFunction() {...};
  
  anotherFunction() {...};
  
  render() {
    return (
      <ChildComponent 
        someFunction={this.someComponent} // context will be bound 
      />
    )
  }
}