Last active
November 14, 2017 17:07
-
-
Save cuperman/2155e7e4839d891d3d88b351f3c8c539 to your computer and use it in GitHub Desktop.
Bind methods to this
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
// 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