Skip to content

Instead of having to manually bind every instance of a method in React Classes, in the constructor this is shorthand for you.

Notifications You must be signed in to change notification settings

dsacramone/smooth-react-class-bind

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

smooth-react-class-bind

Synopsis

Quick way to autobind your React ES6 classes w/o having to manually do it in the constructor.

Usage

Please go to npm, and pick up the package.

  • Import package at top of your page
  • Follow instructions below

// your react imports etc..

import autoBindMethods from 'smooth-react-class-bind'

class yourClass extends Component { constructor() { super()

    autoBindMethods(this)
}

}

Tests

TODO: this is immensely important, but I just didn't have the time to implement atm. I will revisit shortly.

License

MIT


Optional Arguments

append

@array
allows for binding of mixin methods, just pass in method name.

exclude

@array
pass in method names you do not want bound to the this context

include

@array
pass only the methods you want to bind to this. Using this array will take precedence. If you pass
   this in, you will only bind what is in this array - not all the methods on 'this' context.
   You might want to target just one method, or two w/o binding a huge array of methods.

How to use?

note: assuming you installed the package and imported it with the name of: autoBindMethods


*I want to bind all methods in my React Class:

constructor() {
    super()
    autoBindMethods(this)
}


*I want to bind only the method 'getProducts'

constructor() {
    super()
    autoBindMethods(this, {include: ['getProducts']})
}


*I want to exclude only the method 'getProducts', from binding all other methods

constructor() {
    super()
    autoBindMethods(this, {exclude: ['getProducts']})
}


If you notice any issues, please let me know. Thanks!

About

Instead of having to manually bind every instance of a method in React Classes, in the constructor this is shorthand for you.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published