Bind all methods with a single line
npm install react-bind-all-methods
class App extends Component {
constructor() {
super()
// ... initial state
this.method1 = this.method1.bind(this)
this.method2 = this.method2.bind(this)
...
this.methodN = this.methodN.bind(this)
}
}
`
And, using lambda is tricky. It make class definition looks worst.
Even though I love functional programming and mixing OOP/FP,
the one below is still not acceptable.
`
class App extends Component {
constructor() {
super()
// ... initial state
}
method1 = () => {}
method2 = () => {}
...
methodN = () => {}
}
`
So, this react-bind-all-methods is for binding all methods_
_with a single line.
Installation
Yeah, I'm an old school Node user. So npm. No yarn version.
`
npm install --save react-bind-all-methods
`
Usage
`
import bindAllMethods from react-bind-all-methods
class App extends Component {
constructor() {
super()
// ... initial state
bindAllMethods(App)(this)
}
method1() {}
method2() {}
...
methodN() {}
}
``