Last active
January 17, 2017 17:05
-
-
Save jungchris/bbc484f767efbedee61f7e48d159bd00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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