Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-react-binding

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-react-binding

babel plugin for React two-way bind

  • 0.11.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16
increased by1500%
Maintainers
1
Weekly downloads
 
Created
Source

babel-plugin-react-binding

Two way binding sugar for React.

Travis codecov

Usage

install
npm i babel-plugin-react-binding --save-dev
add react-binding to .babelrc
{
  "plugins": [
    ["react-binding", {
      // options
    }]
  ]
}
use binding in React JSX!
<div>
  <input binding={this.state.inputValue}>
  <Dialog binding={this.state.dialogOpen}>
</div>
binding to redux example
class App extends React.Component {
  render () {
    return (
      <input id='input' binding={this.props.input} />
    )
  }
}

export default connect(
  state => state.form,
  dispatch => ({
    onChange (value, key) {
      dispatch({
        type: 'UPDATE_FORM',
        key,
        value,
      })
    },
  })
)(App)

Principle

react-binding will automatically add value and onChange props to React Element. After the event triggered, react-binding will receive the new value, and execute setState() to update the value.

You can think of it as (if you write by hand):

<input
  value={this.state.inputValue}
  onChange={e => this.setState({ inputValue: e.target.value })}
>

Options

Use babel option to custom the prop name (default binding)

{
  "plugins": [
    ["react-binding", {
      "attrName": "bindModel"
    }]
  ]
}

Then the jsx code may be like:

<input bindModel={this.state.inputValue}>

Custom Component

By default, react-binding use value and onChange as default prop names for two-way binding (except radio and checkbox, they use checked). But some custom component may use another prop name, for example onInput.

Use static property bindingDescriptor for adaptation.

CustomComponent.bindingDescriptor = {
  prop: 'number',
  event: 'onInput',
}

Keywords

FAQs

Package last updated on 30 Sep 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc