All Projects → chikara-chan → babel-plugin-react-scope-binding

chikara-chan / babel-plugin-react-scope-binding

Licence: MIT license
🍖 Babel plugin for React component to take event handler to bind context automatically.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to babel-plugin-react-scope-binding

nornj
More exciting JS/JSX based on Template Engine, support control flow tags, custom directives, two-way binding, filters and custom operators.
Stars: ✭ 97 (+361.9%)
Mutual labels:  babel-plugin
babel-plugin-storybook-csf-title
A Babel plugin to generate titles for Storybook CSF stories at compile time, typically based on the story file's file name.
Stars: ✭ 17 (-19.05%)
Mutual labels:  babel-plugin
babel-plugin-object-styles-to-template
Babel plugin to transpile object styles to template literal
Stars: ✭ 33 (+57.14%)
Mutual labels:  babel-plugin
idomizer
An HTML template parser compiling an incremental-dom render factory.
Stars: ✭ 15 (-28.57%)
Mutual labels:  babel-plugin
babel-plugin-solid-undestructure
A Babel plugin for SolidJS that allows you to destructure component props without losing reactivity.
Stars: ✭ 45 (+114.29%)
Mutual labels:  babel-plugin
babel-plugin-react-directives
A babel plugin that provides some directives for react(JSX), similar to directives of vue.
Stars: ✭ 80 (+280.95%)
Mutual labels:  babel-plugin
babel-plugin-rewire-exports
Babel plugin for stubbing [ES6, ES2015] module exports
Stars: ✭ 62 (+195.24%)
Mutual labels:  babel-plugin
babel-plugin-inline-svg
Babel plugin to optimise and inline svg
Stars: ✭ 50 (+138.1%)
Mutual labels:  babel-plugin
babel-plugin-transform-rename-properties
A Babel plugin for renaming JavaScript properties
Stars: ✭ 19 (-9.52%)
Mutual labels:  babel-plugin
babel-plugin-detective
Babel plugin that scans the AST for require calls and import statements
Stars: ✭ 26 (+23.81%)
Mutual labels:  babel-plugin
babel-plugin-transform-for-of-as-array
Transform all for-of loops into the equivalent array for loop
Stars: ✭ 14 (-33.33%)
Mutual labels:  babel-plugin
babel-plugin-solid-labels
Simple, reactive labels for SolidJS
Stars: ✭ 127 (+504.76%)
Mutual labels:  babel-plugin
babel-plugin-transform-amd-to-commonjs
✨ Babel plugin that transforms AMD to CommonJS
Stars: ✭ 44 (+109.52%)
Mutual labels:  babel-plugin
penv.macro
A macro used with babel-plugin-macros to write configurations for multiple environments, and remove configurations are irrelevant with the specified environment from your codes finally.
Stars: ✭ 73 (+247.62%)
Mutual labels:  babel-plugin
sprockets-bumble d
Sprockets plugin to transpile modern javascript using Babel, useful while migrating to ES6 modules
Stars: ✭ 32 (+52.38%)
Mutual labels:  babel-plugin
babel-plugin-hyperscript-to-jsx
This plugin transforms react-hyperscript into JSX. Intended to be used as codemod.
Stars: ✭ 20 (-4.76%)
Mutual labels:  babel-plugin
babel-plugin-console-source
Add the file name and line numbers to all console logs.
Stars: ✭ 38 (+80.95%)
Mutual labels:  babel-plugin
babel-plugin-flow-relay-query
Babel plugin which converts Flow types into Relay fragments
Stars: ✭ 38 (+80.95%)
Mutual labels:  babel-plugin
babel-plugin-transform-hoist-nested-functions
Babel plugin to move nested functions to outer scopes
Stars: ✭ 32 (+52.38%)
Mutual labels:  babel-plugin
babel-plugin-transform-react-qa-classes
Add component's name in `data-qa` attributes to React Components
Stars: ✭ 41 (+95.24%)
Mutual labels:  babel-plugin

babel-plugin-react-scope-binding

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

Travis branch npm npm

Installation

$ npm install babel-plugin-react-scope-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 Header extends React.Component{
  constructor() {
    super()
    this.handleClick = this.handleClick.bind(this) // binding method
  }
  handleClick(e) {
    this.setSate({
      key: 'value'
    })
  }
  render() {
    return (
      <div onClick={this.handleClick}></div>
    )
  }
}

Oh shit! 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 Header extends React.Component{
  constructor() {
    super()
    // needn't binding method
  }
  handleClick(e) {
    this.setSate({
      key: 'value'
    })
  }
  render() {
    return (
      <div onClick={this.handleClick}></div>
    )
  }
}

Usage

Write via babelrc.

// .babelrc
{
  "plugins": [
    ["react-scope-binding", {
      "propPrefix": "on",
      "advanced": true
    }]
  ]
}

Options

Name Type Default Description
propPrefix String | Array 'on' Tell plugin what the JSX attributes need binding, default with 'on' prefix, e.g. onClick, onChange. You can also pass an Array to this option, such as ['on', '...']
advanced Boolean false Enable advanced usage. In some situation, you want to pass value to event handler. With this option enabled, you can easily write a code such as <div onClick={this.handleClick(item)}></div>. Plugin will auto transpile it to <div onClick={(e) => {this.handleClick(e, item)}></div>

Links

https://chikara-chan.github.io/babel-plugin-react-scope-binding/

License

Released under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].