Giter VIP home page Giter VIP logo

babel-plugin-react-auto-binding's Introduction

babel-plugin-react-auto-binding

Babel plugin for React component to take event handler to bind context automatically.

Installation

$ npm install babel-plugin-react-auto-binding --save-dev

Motivation

When you are building a React component, you have to be careful about event handler. In component, class methods are not bound by default. If you forget to bind this.handleClick and pass it to onClick, this will be undefined when the function is actually called.

Therefore, you have to bind the event handler in constructor method, like this,

class App extends React.Component{
  constructor(props) {
    super(props)
    this.state = {
      view: false
    }
    this.handleClick = this.handleClick.bind(this) // binding method
  }
  handleClick(e) {
    this.setSate({
      view: true
    })
  }
  render() {
    return (
      <div>
        <button onClick={this.handleClick}>Click me</button>
        <br />
        {
            (view) && <div>React auto binding succeeded</div>
        }
      </div>
    )
  }
}

It's so troublesome. So, this plugin is born to resolve these thorny problems. With this plugin, you can easily code without caring about context.

Instead,

class App extends React.Component{
  constructor(props) {
    super(props)
    this.state = {
      view: false
    }
    // needn't binding method
  }
  
  handleClick(e) {
    this.setSate({
      view: 'value'
    })
  }
  render() {
    return (
      <div>
        <button onClick={this.handleClick}>Click me</button>
        <br />
        {
            (view) && <div>React auto binding succeeded</div>
        }
      </div>
    )
  }
}

Usage

Write via babelrc.

// .babelrc
{
  "plugins": [
    "react-auto-binding"
  ]
}

Warning

This plugin check the spuerClass after the extends in classDeclaration to know whether it is React component or not.

Only supported React.Component, React.PureComponent, Component, PureComponent

Do not use other words instead of it, like this,

import {Component as ReactComponent} from "react"

class App extends ReactComponent{
  //...
}

babel-plugin-react-auto-binding's People

Contributors

bugkun avatar

Stargazers

 avatar

Watchers

 avatar

babel-plugin-react-auto-binding's Issues

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.