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.
npm install --save react-reactive-context
return a new Context, the function accepts two parameters
name | type | description |
---|---|---|
state | any | default context value |
decorator | func | when the state change take its value and return a new decorated state |
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 |
Consumer props are passed like decorators to decorator function @see Consumer
trigger component update if something changes, accepts two parameters
name | type | description |
---|---|---|
Context | ReactiveContext | Context variable |
decorators | object | passed to decorator function |
accepts two parameters
name | type | description |
---|---|---|
callback | func | return current decorated state |
decorators | object | passed to decorator function |
update a Context, accepts new state value
return current Context status everywhere, accepts a decorator object
remove all subscribtion registered by subscribe method
import React, { useEffect, useState } from "react";
import { createReactiveContext, useReactiveContext } from "react-reactive-context";
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>
//update component by custom hook
const {state, decoratedState} = useReactiveContext(Context, { /* decorators returned inside decorator function */ });
<Context.Consumer /* all props except children are passed like decorators to decorator */>
{({state, decoratedState}) => null}
</Context.Consumer>
useEffect(() => {
//listen to any Context change
const subscription = Context.subscribe(({state, decoratedState}) => {
}, {/* decorators returned inside decorator function */});
return () => {
//unsubscribe
subscription();
};
}, []);
Context.set(state);
or you could update Context.Provider value
const {state, decoratedState} = Context.get(/* decorators returned inside decorator function */);
MIT © andmau90