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

bey-fix

v1.1.0

Published

Simple immutable state for React

Downloads

3

Readme

bey

Simple immutable state for React using Immer

  • ~700B minified and gzipped (not including React & Immer)
  • Try it out alongside other state solutions
  • Heavily inspired by react-copy-write

Install

yarn add bey

Example

import React from 'react';
import { render } from 'react-dom';
import { state, update, Subscribe } from 'bey';

let counter = state({
  count: 0,
});

function increment() {
  update(counter, state => { state.count++ });
}

function decrement() {
  update(counter, state => { state.count-- });
}

function Counter() {
  return (
    <Subscribe to={counter} on={state => state.count}>
      {count => (
        <div>
          <button onClick={decrement}>-</button>
          <span>{count}</span>
          <button onClick={increment}>+</button>
        </div>
      )}
    </Subscribe>
  );
}

render(<Counter/>, window.root);

Run this example locally by cloning the repo and running yarn example in the root directory.

Guide

state()

First you'll need to create some state containers for your app.

let counter = state({
  count: 0
});

Call state() with your initial state and you're good to go.

This returns a small object which we can subscribe to, update, and get the current state from.

update()

You'll want to create some "actions" that will update your state when called. These should just be plain functions that call update() inside.

let counter = state({
  count: 0
});

function increment() {
  update(counter, state => { state.count++; });
}

function decrement() {
  update(counter, state => { state.count--; });
}

When you call update() you'll pass your state container and an "updater" function. Inside the "updater" function is just passed through to Immer (You'll want to read about that works).

You'll notice that inside the updater function you can just mutate whatever you want. Don't mutate outside of the updater function though.

<Subscribe/>

Finally, you'll need a way to subscribe to state updates and re-render your tree.

function Counter() {
  return (
    <Subscribe to={counter}>
      {state => <span>{state.count}</span>}
    </Subscribe>
  );
}

If you only need part of your state, you get pass an optional on prop which selects just the state you care about and passes it through to the children function.

function Counter() {
  return (
    <Subscribe to={counter} on={state => state.count}>
      {count => <span>{count}</span>}
    </Subscribe>
  );
}

This also acts as an optimization when you have a large state object but only need to re-render when a nested value in that state object is updated.

If you need multiple values, you can return an object:

<Subscribe to={user} on={state => ({ name: state.name, username: state.username })}>
  {({ name, username }) => <span>{name} (@{username})</span>}
</Subscribe>

The same optimizations will apply using the same shallow equality check that React uses.

Calling actions

Actions can be called at any time from any part of your app.

let counter = state({ count: 0 });

function increment() {
  update(counter, state => { state.count++; });
}

function Increment() {
  return <button onClick={increment}>+</button>;
}

Multiple instances of containers

If you need to create multiple instances of containers, wrap your state() call in a factory:

let createCounter = () => {
  return state({ count: 0 });
};

Remember that your methods for updating state are just functions, don't be afraid to write them however you need. So here we can pass the state object we are updating as a parameter.

function increment(counter) {
  update(counter, state => { state.count++; });
}

function decrement(counter) {
  update(counter, state => { state.count--; });
}

Then inside your components where you call those methods, pass the state objects in:

function Counter(props) {
  return (
    <Subscribe to={props.counter} on={state => state.count}>
      {count => (
        <div>
          <button onClick={() => decrement(props.counter)}>-</button>
          <span>{count}</span>
          <button onClick={() => increment(props.counter)}>+</button>
        </div>
      )}
    </Subscribe>
  );
}

function Counters(props) {
  return (
    <div>
      <Counter counter={createCounter()}/>
      <Counter counter={createCounter()}/>
    </div>
  );
}