react-bind-all-methods

1.0.1 • Public • Published

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

Package Sidebar

Install

npm i react-bind-all-methods

Weekly Downloads

5

Version

1.0.1

License

MIT

Unpacked Size

3.17 kB

Total Files

6

Last publish

Collaborators

  • codenart