React context API used to replicate a simpler redux state manager
This package aims to help providing a fast replica of redux state manager with a provider and a hook consumer. Wrap you app/component/page with the Provider and its actions/reducers/initialState, and use the hook consumer in any component below.
Only works with react >=16.8
How to use it
Actions
// context/actions.jsconst countActions = id: id type: 'INCREMENT' id: id type: 'DECREMENT' id: id type: 'RESET' count:
Reducers
// context/reducers.jsconst countReducer state type => { return ...state count: }
Provider
// Import your actions and reducers // Your initial provided stateconst providedState = count: 0 isAuth: false const YourEnhancedComp = <Provider value= providedState actions=actions reducers=reducers> <div> You can use the providedState anywhere below the Provider</div></Provider>
Consumer
const YourConnectedComp = { const count isAuth // First object is the providedState increment decrement reset // Second object is the reducers = // First and second array of strings to link the called states and reducers return <section> <button onClick= increment > + </button> <div>Count: count </div> <button onClick= decrement > - </button> <button onClick= reset > Reset </button> <hr /> </section>}