Code Monkey home page Code Monkey logo

react-autobind's Introduction

React Class autoBind

Automatically binds methods defined within a component's Class to the current object's lexical this instance (similarly to the default behavior of React.createClass).

Description

React 0.13 introduced the possibility to define components using plain JavaScript ES6 classes. But differently from the traditional React.createClass, user defined methods in a ES6 class are not automatically bound.

Autobind is an utility function that can be called from the class constructor to bind the class methods to the component instance.

Installation

To install the stable version:

npm install --save react-autobind

Then import it:

import autoBind from 'react-autobind';

Usage

You will generally call autoBind from the class constructor, passing the 'this' context:

constructor(props) {
  super(props);
  autoBind(this);
}

Autobind is smart enough to avoid binding React related methods (such as render and lifecycle hooks).

You can also explicitly specify certain methods to exclude from binding:

constructor(props) {
  super(props);
  autoBind(this, {
    wontBind: ['leaveAlone1', 'leaveAlone2']
  });
}

Or specify the only methods that should be auto-bound:

constructor(props) {
  super(props);
  autoBind(this, {
    bindOnly: ['myMethod1', 'myMethod2']
  });
}

Naturally, wontBind and bindOnly cannot be used together.

Example

import React from 'react';
import {render} from 'react-dom';
import autoBind from 'react-autobind';

class App extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      clickCounter: 0
    }
    autoBind(this);
  }

  increment() {
    this.setState({
      clickCounter: this.state.clickCounter + 1
    });
  }

  // React's render and lifecycle hooks aren't bound

  componentDidMount() {
    console.log("Component is mounted.");
  }

  render(){
    return (
      <div>
        <h1>Number of clicks: {this.state.clickCounter}</h1>
        <button onClick={this.increment}>Increment Counter</button>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

Similar Projects

React-autobind is similar (and forks some code from) Autobind Decorator, with two main differences:

  1. React-autobind is focused on React, and avoids binding React's Component methods such as render and lifecycle hooks.

  2. React-autobind is a plain function call, while Autobind Decorator uses a more elegant but still not standardized JavaScript syntax that is subject to change in the future.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.