class-autobind

WebJar for class-autobind

License

License

ISC
Categories

Categories

Auto Application Layer Libs Code Generators
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

class-autobind
Last Version

Last Version

0.1.4
Release Date

Release Date

Type

Type

jar
Description

Description

class-autobind
WebJar for class-autobind
Project URL

Project URL

https://www.webjars.org
Source Code Management

Source Code Management

https://github.com/kodefox/class-autobind

Download class-autobind

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/class-autobind/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>class-autobind</artifactId>
    <version>0.1.4</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/class-autobind/
implementation 'org.webjars.npm:class-autobind:0.1.4'
// https://jarcasting.com/artifacts/org.webjars.npm/class-autobind/
implementation ("org.webjars.npm:class-autobind:0.1.4")
'org.webjars.npm:class-autobind:jar:0.1.4'
<dependency org="org.webjars.npm" name="class-autobind" rev="0.1.4">
  <artifact name="class-autobind" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='class-autobind', version='0.1.4')
)
libraryDependencies += "org.webjars.npm" % "class-autobind" % "0.1.4"
[org.webjars.npm/class-autobind "0.1.4"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

Method auto-bind for ES6 (ES2015) classes

This package provides a single function, autobind, for use within a constructor to bind all methods to the instance itself.

For example, this allows us to pass a method to an event handler element.addEventListener('click', this.onClick) and be sure the onClick method will always be called with the right context.

Note: This has some specific logic for React, but could be used in any project.

Installation:

npm install --save class-autobind

Usage:

import autobind from 'class-autobind';

class MyComponent extends React.Component {
  constructor() {
    super(...arguments);
    autobind(this);
  }
  render() {
    return <button onClick={this.onClick}>Click Me</button>;
  }
  onClick() {
    console.log('Button Clicked');
  }
}

Advanced Usage:

If your component will possibly be subclassed (you really should not do this, but some third-party libraries like react-css-modules do so) then you will need to specify which prototype will be the source of methods that are to be automatically bound.

import autobind from 'class-autobind';

class MyComponent extends React.Component {
  constructor() {
    super(...arguments);
    autobind(this, MyComponent.prototype); // Note the second parameter.
  }
  render() {
    /* ... */
  }
}

class MySubClassedComponent extends MyComponent {
  /* This is probably a very bad idea. */
}

License

This software is BSD Licensed.

org.webjars.npm

KodeFox

Versions

Version
0.1.4