1.1.8 • Published 9 months ago

react-reactive-context v1.1.8

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

react-reactive-context

NPM JavaScript Style Guide

Table of Contents

About

The purpose of this module is increase the functionality of React.Context without add into the project a big library like redux. This module work in the same way of base React.Context but adds the possibility to set a new value without update Context.Provider prop, also allows to read Context state where you want into the code, there is also the possibility to add subscribers called that will be called when an update of Context occurs. Finally, the creator function or Context.Provider component accept a decorator that receives a current Context state and a set of attributes from each reader (hook, Consumer, subscriber) to customize the state for each of them.

Installing

npm install --save react-reactive-context

Doc

createReactiveContext

return a new Context, the function accepts two parameters

nametypedescription
stateanydefault context value
decoratorfuncwhen the state change take its value and return a new decorated state

Context.Provider

Provider accepts the following props @seeProvider | name | type | description | | --------- | ---- | ------------------------------------------------------------------------------------------ | | value | any | context value | | decorator | func | when the state change take its value and return a new decorated state |

Context.Consumer

Consumer props are passed like decorators to decorator function @see Consumer

useReactiveContext

trigger component update if something changes, accepts two parameters | name | type | description | | ---------- | ---------------- | --------------------------------------------------------------------------------- | | Context | ReactiveContext | Context variable | | decorators | object | passed to decorator function |

subscribe

accepts two parameters | name | type | description | | ---------- | ---------------- | --------------------------------------------------------------------------------- | | callback | func | return current decorated state | | decorators | object | passed to decorator function |

set

update a Context, accepts new state value

get

return current Context status everywhere, accepts a decorator object

removeAllSubscribers

remove all subscribtion registered by subscribe method

Usage

Import

    import React, { useEffect, useState } from "react";
    import { createReactiveContext, useReactiveContext } from "react-reactive-context";

Initialization

    type MyContext = {
        color: string,
        supportColor?: string
    };

    type MyContextDecorated = {
        ...
    };

    type MyDecorators = {};

    //Context initialization, could be empty
    const Context = createReactiveContext<MyContext, MyContextDecorated, MyDecorators>({
        supportColor: "#AA0000"
    }, (state) => processedState);

    <Context.Provider value={state} decorator={(state) => processedState}>
        {children}
    </Context.Provider>

Hook

    //update component by custom hook
    const {state, decoratedState} = useReactiveContext(Context, { /* decorators returned inside decorator function */ });

Consumer

    <Context.Consumer /* all props except children are passed like decorators to decorator */>
        {({state, decoratedState}) => null}
    </Context.Consumer>

Subscriber

   useEffect(() => {
        //listen to any Context change
        const subscription = Context.subscribe(({state, decoratedState}) => {
            
        }, {/* decorators returned inside decorator function */});

        return () => {
            //unsubscribe
            subscription();
        };
    }, []);

Setter

    Context.set(state);

or you could update Context.Provider value

Getter

    const {state, decoratedState} = Context.get(/* decorators returned inside decorator function */);

License

MIT © andmau90

1.1.1

9 months ago

1.1.0

9 months ago

1.1.8

9 months ago

1.0.9

10 months ago

1.1.7

9 months ago

1.0.8

10 months ago

1.1.6

9 months ago

1.0.7

10 months ago

1.1.5

9 months ago

1.1.4

9 months ago

1.1.3

9 months ago

1.1.2

9 months ago

1.0.10

10 months ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago