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

react-app-context

v1.0.6

Published

Application state manager based on PureComponent and React Context Api (React >= 16). Like Redux

Readme

react-app-context

Application state manager based on PureComponent and React Context Api (React >= 16). Like Redux.

React context: https://reactjs.org/docs/context.html

Installation

npm i --save react-app-context

What is it?

It is one function

import initStorage from 'react-app-context';

//...
const {
  Provider,
  connect,
  Context
} = initStorage(
  defaultState,
  actions,
  property
);
// ...

Arguments

defaultState - object, which contains initialize state

{
  app: { ... },
  users: { ... },
  categories: { ... }
  // ...
}

actions - object, which contains actions functions. Each action will get state as first argument and should return new object, which will be put to context object. Action can be async function or return Promise.

{
  app: {
    // all actions in this path will get Context.app state part
    setName: (state, newName) => ({ ...state, name: newName })
    // ...
  },
  users: {
    // all actions in this path will get Context.users state part
    addUser: (state, name, login, pass) => { ... }
    // ...
  }
  // all actions in root will get full context state object
  removeStatePart: (fullState, key) => ({ ...fullState, [key]: null })
  // ...
}

property - object with options

{
  debug: false // if true, debug then messages will put to console
}

Function result

This function returns object with

{
  Provider, // react component
  connect, // function to connect your react component to state
  Context // React Context Api object
}

Provider - simple React pureComponent, which shoud be around components, which will be get state of this Provider.

// ...
  <Provider>
    <MainComponentOfApp />
  </Provider>
// ...

connect - it is HOC function, which returns PureComponent above your component.

function(getNewState: function(state, props), dispatchActions: object): function(Component)
  • getNewState - (state, props) => ({ ... })
  • dispatchActions - { myAction, ... }
// ...
  export default connect(
    (state, props) => ({
      value: state.app.value,
      isFetching: state.app.requests[props.id].isFetching
    }),
    { myAction }
  )(MyComponent);
// ...

How can you use it as application state like Redux?

Storage initialize

// storageConfig.js
import initStorage from 'react-app-context';
import appStorage, { actions } from './appStorage.js';

const Storage = initStorage(
  { app: appStorage },
  { app: actions },
  { debug: false }
);

export {
  connect: Storage.connect
}

export default Storage.Provider;

Application storage

// appStorage.js
export default const initState = {
  value: 10,
  list: [],
  state: 'init',
  isLoading: false
};

export function setValue(state, newValue) {
  return {
    ...state,
    value: newValue
  };
}

export async function getData(state) {
  // it is your api function, for example
  // `this` will contains
  // {
  //   dispatch: function(function, statePropKey: string): function, - generates function with selected latest state
  //   actionsMap: Map[function, function], - contains all registered Actions
  //   call: function(function, ...args) - use to call Action
  //   getState: function: object - get latest state
  //   setState: function: Promise - works like setState of Component/PureComponent, but can be called like `await this.setState({ ... });`
  // }
  dispatch(state => ({ ...state, isLoading: true }), 'app')();
  const newList = await myApi.getList();
  return {
    ...state, // or ...this.getState()
    list: newList,
    isLoading: false
  };
}

export const actions = {
  setValue,
  getData
};

App root component

// App.jsx
import React from 'react';
import Provider from './storageConfig';
import Toolbar from './Toolbar/Toolbar';

function App() {
  return (
    // all Providers childs can use `connect` function from `./storageConfig`
    <Provider>
      <Toolbar />
    </Provider>
  );
}

export default App;

Example child component

// MyButton.jsx
import React from 'react';
import { connect } from './storageConfig';
import { setValue } from './appStorage';

class MyButton extends React.PureComponent {
  // `setValue` was described like `(state, newValue) => ...`
  // but will be dispatched into component without state arg
  // like `(newValue) => ...`
  onClick = () => this.props.setValue(Math.round(Math.random() * 1000));

  render() {
    const { value } = this.props;

    return (
      <button type="button" onClick={this.onClick}>
        {value}
      </button>
    )
  }
}

export default connect(
  state => ({ value: state.app.value }),
  // setValue will be put into component without first argument (it was `state`)
  { setValue }
)(MyButton);

Using as state of component

react-app-context can be used as external component state