Skip to content

Instantly share code, notes, and snippets.

@cuperman
Last active November 14, 2017 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cuperman/2155e7e4839d891d3d88b351f3c8c539 to your computer and use it in GitHub Desktop.
Save cuperman/2155e7e4839d891d3d88b351f3c8c539 to your computer and use it in GitHub Desktop.
Bind methods to this
// Usage:
//
// import bindAllMethods from 'bind_all_methods';
// import React from 'react';
//
// class MyComponent extends bindAllMethods(React.Component) {
// ...
// }
//
export default function bindAllMethods(BaseComponent) {
class BoundComponent extends BaseComponent {
constructor() {
super();
Object.getOwnPropertyNames(Object.getPrototypeOf(this)).filter(m => m !== 'constructor').forEach(m => this[m] = this[m].bind(this), this);
}
}
return BoundComponent;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment