0.1.1 • Published 6 years ago

babel-plugin-auto-binder v0.1.1

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

babel-plugin-auto-binder

Maintainability

Inspiration:

Every time I write a new component method I have to bind in the constructor so that it can access this.

Example: this.doStuff = this.doStuff.bind(this);

Now if you forget to bind your method, you will face very weird bugs which can be frustating. babel-plugin-auto-binder will take care of this issue by doing this binding behind the scene so you just worry about the functionality.

If you are using or prefer using arrow functions then you don't need this plugin as the arrow function have the this context from the scope they are declared in.

Performance:

This plugin binds your class methods (does not binds react lifecycle methods in React) only once in constructor which is a big performance plus.

Getting Started

Setting up

  1. npm i -D babel-plugin-auto-binder
  2. npm i -D babel-plugin-transform-decorators-legacy
  3. Update your .babelrc configuration by adding
{
  "plugins" : [
    "auto-binder",
    "transform-decorators-legacy",
  ]
}

Usage

Decorate your class with @autobind decorator like this:

@autobind
class App extends Component {
  constructor() {
    super();
  }

  doStuff() {
    //some api calls
  }

  render() {
    return (
      <div>
        <SomeComponent onSubmit={this.doStuff} />
      </div>
    )
  }
}

and that's it!!

The above input will get transformed to

class App extends Component {
  constructor() {
    super();

    this.doStuff = this.doStuff.bind(this);
  }

  doStuff() {
    //some api calls
  }

  render() {
    return (
      <div>
        <SomeComponent onSubmit={this.doStuff} />
      </div>
    )
  }
}
0.1.1

6 years ago

0.1.0

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago