Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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
You can’t perform that action at this time.