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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@menseb/react-context-reducer

v1.1.2

Published

React Context Reducer

Downloads

87

Readme

Build Status CodeFactor Coverage Status David David Contributor Covenant NPM NPM npm bundle size (scoped)

Table of contents

Installation

npm i @menseb/react-context-reducer

How it works

The main goal of this package was to avoid having to define your own reducer function using a javascript switch. Instead, it creates the reducer function using your own actions.

Each of your action should be a function which receives the current state and additional arguments and returns an updated state.

Example :

export function myAction(state, arguments) {
    const { bar } = state;
    const { foo } = arguments;

    return { ...state, bar: foo ? foo : bar };
}

You may also provide an initial state and a configuration function to give you more flexibility on your initial setup.

Example :

export const initial = {
    bar: false
};

export function config(initial) {
    const { bar } = initial;

    if (bar) return initial;

    return { ...initial, bar: !bar };
}

This package will use the actions, the initial state and the configuration with the reducer hook from React, useReducer, to create the state and dispatch. Then, it will enhance the dispatch function by binding it to your actions names.

Example :

// Instead of using dispatch this way
dispatch({ type: 'myAction', foo: true });

// You would use it this way
dispatch.myAction({ foo: true });

Finally, the Provider in this package will expose dispatch and state, which you can consume using the different ways below.

How to use

Provider

import { Provider } from '@menseb/react-context-reducer';
import { config, initial } from 'path/to/utilities';
import * as actions from 'path/to/actions';

export default function MyProvider({ children }) {
    return (
        <Provider actions={actions} config={config} initial={initial}>
            {children}
        </Provider>
    );
}

Consumers

  • Consumer

import { Component } from 'react';
import { Consumer } from '@menseb/react-context-reducer';

export default class MyConsumer extends Component {
    render() {
        return (
            <Consumer>
                {({ dispatch, state }) => {
                    // Use dispatch and state
                }}
            </Consumer>
        );
    }
}
  • ConsumerState

import { Component } from 'react';
import { ConsumerState } from '@menseb/react-context-reducer';

export default class MyConsumerState extends Component {
    render() {
        return (
            <ConsumerState>
                {(state) => {
                    // Use state
                }}
            </ConsumerState>
        );
    }
}
  • ConsumerDispatch

import { Component } from 'react';
import { ConsumerDispatch } from '@menseb/react-context-reducer';

export default class MyConsumerDispatch extends Component {
    render() {
        return (
            <ConsumerDispatch>
                {(dispatch) => {
                    // Use dispatch
                }}
            </ConsumerDispatch>
        );
    }
}

Hooks

  • useContextReducer

import { useContextReducer } from '@menseb/react-context-reducer';

export default function MyHook() {
    const { dispatch, state } = useContextReducer();

    // Use dispatch and state

    return (...);
}
  • useContextState

import { useContextState } from '@menseb/react-context-reducer';

export default function MyHookState() {
    const state = useContextState();

    // Use state

    return (...);
}
  • useContextDispatch

import { useContextDispatch } from '@menseb/react-context-reducer';

export default function MyHookDispatch() {
    const dispatch = useContextDispatch();

    // Use dispatch

    return (...);
}

PropTypes

  • Provider props

| property | type | required | default | |----------|------|----------|---------| | actions | Object of Function | true | none | | children | React Node | true | none | | config | Function | false | undefined | | initial | Object | false | {} |

  • Consumers props

| property | type | required | default | |----------|------|----------|---------| | children | Function | true | none |

  • Hooks props

None, but you must follow the Rules of Hooks from React.

Scripts

See scripts from the package.json file.

Code of conduct

See code of conduct from the CODE_OF_CONDUCT.md file.

License

See license from the LICENSE.md file.