1.1.2 • Published 3 years ago

@menseb/react-context-reducer v1.1.2

Weekly downloads
16
License
ISC
Repository
github
Last release
3 years ago

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

propertytyperequireddefault
actionsObject of Functiontruenone
childrenReact Nodetruenone
configFunctionfalseundefined
initialObjectfalse{}
  • Consumers props

propertytyperequireddefault
childrenFunctiontruenone
  • 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.

1.1.1

3 years ago

1.1.2

3 years ago

1.1.0

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago