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

estates

v1.0.1

Published

a tiny (not global) state machine for react trees

Downloads

6

Readme

estates

estates size Build and Test npm


  • TINY (0.5kb) 🔥
  • Written in Typescript
  • Typings for autocomplete automatically shipped.
  • Internally uses React.Context and useState
  • SSR support

Installation

yarn add estates
npm install estates --save

How does it work?

Essentially it's a modifyable react context. It uses react's context API and states to store and edit the data. It uses immer (shipped with redux-toolkit) to make sure that the data is immutable.

What it is

  • A tiny state machine for component trees.
  • Editable react contexts

What it is not

  • A replacement to redux, mobx etc.
  • A global state machine (although it can, but it's not what it's made for)

Why?

I wanted to have a tiny state machine for component trees rather than a global state or drilling props. This is a great solution for components that are dependent on multiple smaller components but where they all need to share the same state.

How to use it

Imagine the following folder structure:

Counter
├── Counter.state.ts
├── Counter.tsx
├── Count.tsx
├── CountButtons.tsx
└── index.ts
// Counter.state.ts

export const CounterEstate = createEstate({
  initialState: {
    count: 0,
  },
  actions: {
    increment(state) {
      state.count++;
    },
    decrement(state) {
      state.count--;
    },
    setCount(state, by: number) {
      state.count = by;
    },
  },
});

// ====================
// Counter.tsx

function Counter() {
  return (
    <CounterEstate.Root>
      <Count />
      <Buttons />
    </CounterEstate.Root>
  );
}

// ====================
// Count.tsx

function Count() {
  const {
    state: { count },
  } = useEstate(CounterEstate);

  return <div>count: {count}</div>;
}

// ====================
// CountButtons.tsx

function CountButtons() {
  const { increment, decrement, setCount } = useEstate(CounterEstate);

  return (
    <div>
      <button onClick={() => increment()}>+</button>
      <button onClick={() => decrement()}>-</button>
      <button onClick={() => setCount(5)}>Set to 5</button>
    </div>
  );
}

Connect components

Sometimes you have components from other libraries, like UI libraries etc., that you want to connect to the estate without having to create a new component for it. You can do this by using the connect function.

connect() will omit the props you pass in the map function and pass the rest to the connected component, so you can use it like you would normally use it, but without having to pass the already mapped props.

import { CounterEstate } from './Counter';
import MuiButton from '@mui/material/Button';

const Button = CounterEstate.connect(MuiButton, (state, actions) => ({
  onClick: actions.increment,
  children: `Count is ${state.count}`,
}))

function Buttons() {
  return (
    <div>
      <Button />
    </div>
  );
}

How it can be used

Compared to a global state where this could get a bit annoying with props drilling and internal state management, this is a little more declarative.

In this example we render three Counters (see above). They all have their own context and it's children can read/edit the state for that tree.

function App(){
  return (
    <div style={{ display: "flex", gap: 40 }}>
      <Counter />
      <Counter />
      <Counter />
    </div>
  );
}

estates counters

Code examples

TODO

Feel free to help implement these features by opening pull requests

  • [ ] HOC's for Root and a connector for class components.