Skip to content

Instantly share code, notes, and snippets.

@jungchris
Last active January 17, 2017 17:05
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 jungchris/bbc484f767efbedee61f7e48d159bd00 to your computer and use it in GitHub Desktop.
Save jungchris/bbc484f767efbedee61f7e48d159bd00 to your computer and use it in GitHub Desktop.
// I love this way of creating a superclass as a bind helper method,
// rather than binding in the constructor as follows:
/*
constructor() {
super();
this. _handleClick = this. _handleClick.bind(this);
this. _handleFoo = this. _handleFoo.bind(this);
}
*/
// we bind in the BaseComponent instead using forEach with a fat arrow function
// Based on: http://www.newmediacampaigns.com/blog/refactoring-react-components-to-es6-classes
class BaseComponent extends React.Component {
_bind(...methods) {
methods.forEach( (method) => this[method] = this[method].bind(this) );
}
}
class ExampleComponent extends BaseComponent {
constructor() {
super();
this._bind('_handleClick', '_handleFoo');
}
}
// these are the bound handlers
_handleClick() {
console.log(this);
// handle click
}
_handleFoo() {
// handle foo
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment