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 🙏

© 2026 – Pkg Stats / Ryan Hefner

enstate

v0.1.3

Published

React state management with Context API

Readme

Enstate

React state management library using Context API. With selectors, actions and middlewares.

⚠ Experimental ⚠

Getting started

Install using either NPM or Yarn:

npm install enstate
yarn add enstate

Usage:

import React from 'react';
import { StateProvider, Container } from 'enstate';

const actions = {
    increment: () => ({
        type: 'increment',
        reduce: state => ({ count: state.count + 1 })    
    }),
};

function App () {
    return (
        <StateProvider initialState={{ count: 0 }}>
            <div>
                <h1>Example App</h1>
                <Container actions={actions}>
                    {({ state, increment }) => (
                        <button onClick={increment}>Update state ({state.count})</button>
                    )}
                </Container>
            </div>
        </StateProvider>
    );
}

To run the examples from this repo locally:

npm start

API

StateProvider

Provider component that holds the application state and update mechanics.

initialState: Object

Defines the initial state of your app.

<StateProvider initialState={{ greeting: 'hello' }}>
    <Container>
        {({ state }) => <h1>{state.greeting}</h1>}
    </Container>
</StateProvider>

middlewares: Array<Function>

Defines the middlewares that will be called when an action gets dispatched.

IMPORTANT: If you use this prop you have to explicitly pass all middlewares to it. Even the default one, that handles your action objects. This gives you total control of how actions are defined and processed.

Here's an example that uses the default action middleware and a custom persistence middleware:

import { StateProvider, actionMiddleware } from 'enstate';

// very naïve persistence middleware
const persistenceMiddleware = provider => next => action => {
    if (typeof action === 'object') {
        localStorage.setItem('appState', JSON.stringify(provider.getState()));
    }
    
    return next(action);
};

const initialState = JSON.parse(localStorage.getItem('appState')) || {};

const middlewares = [actionMiddleware, persistenceMiddleware];

const App = () => (
  <StateProvider initialState={initialState} middlewares={middlewares}>
      ...
  </StateProvider>
);
Included middlewares

The package comes with the following middlewares out of the box:

actionMiddleware Expects dispatched action objects with a type string and a reduce function.

thunkMiddleware Expects dispatched functions for handling side effects (like data fetching and other async stuff). The function receives an object { dispatch: Function, getState: Function }.

devtoolsMiddleware Support for Redux Devtools extension. Disabled by default.

Container

Container components gain access to the state and the connected selectors and actions via render props.

<Container>
    {({ state }) => <span>{state.someValue}</span>}
</Container>

selectors: Object

Selectors are higher order functions that get connected to the state inside the Container component. The first function expects (optional) parameters and the second function receives the state. When you use the selector via render props the state is already injected.

const selectors = {
    getUserById: id => state => state.users[id]   
};

...

<Container selectors={selectors}>
    {({ getUserById }) => <span>{getUserById('foo')}</span>}
</Container>

actions: Object

Actions can be everything that your middlewares can work with. Most common usage is with objects. When you use the default actionMiddleware the object should contain a type string and a reduce function. This function receives the state and should return a new state.

const actions = {
    increment: () => ({
        type: 'increment',
        reduce: state => ({ count: state.count + 1 })    
    }),
};

...

<Container actions={actions}>
    {({ state, increment }) => (
        <button onClick={increment}>Update state ({state.count})</button>
    )}
</Container>

onMount: Function

A prop that can be used to perform a function when the Container component mounts.

Here's an example of two nested containers. One that connects a thunk and one that calls the thunk when mounted:

const actions = {
    fetchItems: () => ({ dispatch }) => {
        // fetch some async data and dispatch an action to update state
    },
};

...

<Container actions={actions}>
    {({ state, fetchItems }) => (
        <Container onMount={fetchItems}>
            {state.items.map(item => (<div>{item.name}</div>))}
        </Container>
    )}
</Container>

onUnmount: Function

A prop that can be used to perform a function when the Container component unmounts.