npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-bind

v0.0.2

Published

Functional reactive 2-way binding

Downloads

6

Readme

React-Bind

Functional reactive 2-way binding.

TL;DR:

import React, { Component } from 'react';
import { Binder } from 'react-bind';

class TextBox extends Component {
  render() {
    return <input type="text" value={this.props.model.state || ""} 
      onChange={ev => this.props.model.set(ev.target.value)} />
  }
}

class Address extends Component {
  constructor() {
    super();
    this.bind = new Binder(this).bind;
  }
  render() {
    return <div>
      <div>Address 1: <TextBox model={this.bind('address1')} /></div>
      <div>Address 2: <TextBox model={this.bind('address2')} /></div>
      <div>City: <TextBox model={this.bind('city')} /></div>
      <div>State: <TextBox model={this.bind('state')} /></div>
      <div>Postal: <TextBox model={this.bind('postal')} /></div>
    </div>
  }
}

class App extends Component {
  constructor() {
    super();
    this.binder = new Binder(this);
    this.bind = this.binder.bind;
    this.state = {
      model: {
        billingAddress: {
          address1: "123 React Blvd",
          address2: "Unit A",
          city: "San Reacto",
          state: "PO",
          postal: "00123"
        }
      }
    };
    this.binder.setModel(this.state.model);
  }

  render() {
    return (
      <div>
        <div>
          Billing Address:<br />
          <Address model={this.bind('billingAddress')} />
        </div>
        <div>
          Shipping Address:<br />
          <Address model={this.bind('shippingAddress')} />
        </div>
      </div>
    );
  }
}

How does it work?

A component carries a model in state. Child components are bound to the model using a binder. The binder carries a portion of the model and allows for getting and setting of properties. When a property is set on the model, the changes are made on a copy, and delivered to the root component to handle. By default, the behavior is to write the new model to state, overriding the previous state. This way, the root component has full control of the model, and there is no data flowing backwards.

Why two-way binding?

React does many things well, but one thing I've found it does not do well is large forms such as registration forms. One key example is creating a reusable street address control for a form that has two or more addresses such as billing vs. shipping. Traditionally, with the many text fields, you have to write individual handlers for each, and set state on a component that doesn't own the data, or provide events to the parent component so it can set the appropriate state per address. With react-bind, as shown in the example above, you only need to create this function once when rendering a native component. You'll notice the example achieves collecting information about both addresses without any more single-use functions.