1.0.1 • Published 6 years ago

react-bind-all-methods v1.0.1

Weekly downloads
2
License
MIT
Repository
-
Last release
6 years ago

React Bind All Methods

So, I'm falling in love with React.
But, binding all the methods of a class is so ... ugh.. ugly.

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() {}
}