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

cursors

v0.8.3

Published

Maintain your React state with Cursors.

Downloads

27

Readme

Cursors

Maintain your React state with Cursors.

Cursors is a React mixin that is inspired by David Nolen's Om. This is a much lighter implementation that is focused on Nolen's idea of a single global state and using cursors to create smaller local states within a single shared data structure.

Cursors leverages the Immutability Helpers provided by React Add-ons. By avoiding mutation, tasks like undo/redo become trivial, reasoning about problems becomes easier, and bugs are easier to avoid.

Install

bower install cursors

API

Top-Level

Cursors

The Cursors object itself should be mixed-in to all Cursors-using components.

Component-Level

this.getCursor(key, [path])

Returns a new cursor with its path set to key's path concatenated with path. key and path should both be a string or number. Use an array of strings and/or numbers for path if your path goes more than one level deep.

this.update(deltas)

Update the state with the change definitions in deltas. For delta syntax check out React's Immutability Helpers.

Examples

Check out the test file for a full example. Here's the basics:

var User = React.createClass({

  // First, mixin Cursors to add the appropriate functions to this component
  // definition.
  mixins: [Cursors],

  // In order for state changes to be recognized globally, you should never need
  // to use `this.setState`. Instead, use `this.update`. `update` takes a key
  // and a delta object. Check the "Immutability Helpers" link for more
  // information. By using `update`, the value will be updated at the root level
  // of the cursor, and changes will be propagated down back to the children.
  // This is a huge win for cursors, because it removes the need to nest
  // callbacks down for changes to objects that live higher up in the hierarchy.
  handleChange: function (ev) {
    this.update({user: {name: {$set: ev.target.value}}});
  },

  // The value of any cursors passed into this component will be reflected in
  // the `this.state` object. This interface allows children to not depend on
  // being passed cursors, but use them transparently if they are passed.
  render: function () {
    return <input value={this.state.user.name} onChange={this.handleChange} />;
  }
});

var Users = React.createClass({

  // First, mixin Cursors to add the appropriate functions to this component
  // definition.
  mixins: [Cursors],

  // The only component that should need to define `getInitialState` is the root
  // component. Child components can define their initial state, but state that
  // is passed into them via parent cursors will override the corresponding
  // initial state.
  getInitialState: function () {
    return {
      users: this.props.users || []
    };
  },

  // When rendering child components, always pass the appropriate `cursor` for
  // the child component via `this.getCursor(key, [path])`.
  renderUser: function (user, i) {
    return <User cursors={{user: this.getCursor('users', i)}} />;
  },

  render: function () {
    return <div>{this.state.users.map(this.renderUser)}</div>;
  }
});

React.renderComponent(
  <MyUsersComponent users={[{name: 'Casey'}, {name: 'Gunner'}]} />,
  document.body
);