Please note that this will add implicit magic to your code
This plugin will autobind all handlers for components listed in directive '@autobind'
prefixes = on, _on, handle, _handle;
In
'@autobind Component';
class Component {
constructor() {
}
handleMe(){}
onMe(){}
noBind(){}
// @autobind-ignore
onIgnored(){}
}Out
class Component {
constructor() {
this.handleMe = this.handleMe.bind(this);
this.onMe = this.onMe.bind(this);
}
handleMe() {}
onMe() {}
noBind() {}
onIgnored() {}
}$ npm install babel-plugin-class-autobindif no components are listed in directive - all handlers in file classes will be bound to class instance. (equal to @autobind *)
.babelrc
{
"plugins": ["class-autobind"]
}$ babel --plugins class-autobind script.jsrequire("babel-core").transform("code", {
plugins: ["class-autobind"]
});