Skip to content

Instantly share code, notes, and snippets.

@ArcanoxDragon
Created May 5, 2018 06:10
Show Gist options
  • Save ArcanoxDragon/d6fe3ecba7b76d9444695727748e0621 to your computer and use it in GitHub Desktop.
Save ArcanoxDragon/d6fe3ecba7b76d9444695727748e0621 to your computer and use it in GitHub Desktop.
React method binding decorator
export function BindMethod( target: any, propertyName: string, descriptor: PropertyDescriptor ) {
let unboundFunc = descriptor.value;
return {
configurable: true,
get() {
let boundFunc = unboundFunc.bind( this );
// Define instance property so we don't have to re-bind it every time the function is accessed
Reflect.defineProperty( this, propertyName, {
value: boundFunc,
configurable: true,
writable: true
} );
return boundFunc;
}
} as PropertyDescriptor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment