- Notifications
You must be signed in to change notification settings - Fork0
Fully native global state manager using react Hooks. You can use this library instead of Redux or Mobx. also you can access state from all of your components and behavior with them using hooks.
License
intellidev1991/react-redux-global-state-manager
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Fully nativeglobal state manager using reactHooks (React > 16.8). You can use this library instead of Redux or MobX. also you can access state from all of your components and behavior with them using hooks.
A nice state management lib for React that uses the React's useState hook. Which basically means no magic behind the curtains, only pure react APIs being used to share state across components.
yarn add react-redux-global-state-manager
npm i react-redux-global-state-manager
We useRedux withReducer andActions pattern with object reference access to whole actions and stores using single import object (IntelliSense work perfectly with it in VSCode)
Create these directories structure in your app for state management:
store/actions/store/stores/store/index.jsstore/storeActions.jsstore/storeNames.js
Create each store separately instore/stores/ directory according meaningful name.
e.g: Create file for store token information in it >store/stores/token.store.js
Then add exported name inside
storeNames.jsfor use Intellisense friendly accessible. I will explain this way in step 3.
e.g:store/stores/token.store.js
import{createStore}from"react-redux-global-state-manager";// List of Constant Typesconsttypes={SET_TOKEN_INFO:"SET_TOKEN_INFO",};// define store with name,initialState and reducerconsttoken=createStore("token",// unique name for this token store{// initialize state objecttoken:null,public_key:"",expire_in:"",login_time:""},(state,action)=>{// define reducer// when a reducer is being used, you must return a new state valueswitch(action.type){casetypes.SET_TOKEN_INFO:return{ ...state, ...action.payload};default:returnstate;}});export{token,types};//export token reference and types
Create separate action file for each store instore/stores/ directory according to your store with meaningful name.
e.g: Create file for store token actions in it >store/actions/token.action.js
Then add exported name inside
storeActions.jsfor use Intellisense friendly accessible. I will explain this way in step 3.
e.g: store/actions/token.actions.js
import{types}from"../stores/token.store.js";consttoken={setTokenInfo:data=>{return{type:types.SET_TOKEN_INFO,payload:data};}};export{token};
Note: follow the files format similar to others files inside
actionsandstoresdirectories.
e.g: addstore/stores/token.store.js intostore/storeNames.js*
import{token}from"./stores/token.store.js";import{profileInfo}from"./stores/profileInfo.store.js";// ------ use this object as store identifier invoker in whole appconststoreNames={ token, profileInfo,};// ------export{storeNames};// export this object as container for all of our stores.
e.g: addstore/actions/token.action.js intostore/storeActions.js
import{token}from"./actions/token.action.js";import{profileInfo}from"./actions/profileInfo.action.js";// ------ use this object as action function invoker in whole appconststoreActions={ token, profileInfo,};// ------export{storeActions};// export this object as container for all of our Actions.
Createstore/index.js and add these code into it.
import{useStore,readOnlyStore,dispatchDirectly}from"react-redux-global-state-manager";import{storeNames}from"./storeNames.js";// All stores referencesimport{storeActions}from"./storeActions.js";// All actions references// ------export{storeNames,storeActions,useStore,readOnlyStore,dispatchDirectly};
Callstore/index.js in your main index.js file once time. (the first begin of your app)
e.g: Main index.js of your app
import"./store/index.js";// load stores once time in begin of your app
import{storeNames,// access all stores referencestoreActions,// access all actions functionsuseStore,// hook - use this in functional component and return [state,dispatch]readOnlyStore,// just return current store value in non-component functionsdispatchDirectly// To access dispatch in non-component functions}from"../store/index";
const[state,dispatch]=useStore(storeNames.token);const[profileInfo,dispatch_pi]=useStore(storeNames.profileInfo);// To call dispatch: dispatch() just take one object. best-practice is to call action functionconstdata={token:"402790c1-ec61-4b10-9613-6b926529d0f2",public_key:"24d6e775def2404fb21a34fdd8a4e4b0",expire_in:"2019-12-14T22:39:10.222Z",login_time:"2019-12-13T10:20:15.222Z"}dispatch(storeActions.token.setTokenInfo(data))
Or use
destructuringproperties:
const[{token,public_key,expire_in},dispatch]=useStore(storeNames.token);
If in a situation you (had to)forced toread data from store ordispatch some data in it, you can use this approach.
//read whole token object out of component function, using store name referenceconsttoken=readOnlyStore(storeNames.token);
//call dispatch function directly out of component functions. this function take 2 params.//arg1= store reference,//arg2= action function that return object.dispatchDirectly(storeNames.token,storeActions.token.setTokenInfo(tokenData));
| Function | Description | Return value |
|---|---|---|
| useStore(storeReference) | This function take yourstore reference to access thecurrent state anddispatch function. Each time the state of the current store going to change, every component that useduseStore() will bere-render. | [stateObject,dispatchFunction] |
| createStore(storeName,initState,reducer) | This function create a new store and return the reference obj to this store. we use this reference to access this store in future | Reference to this store |
| readOnlyStore(storeReference) | Just return current store value in non-component functions | Current state of specific store |
| dispatchDirectly(storeReference,action) | To access dispatch in non-component functions | null |
This library inspired byhttps://github.com/jhonnymichel/react-hookstore
About
Fully native global state manager using react Hooks. You can use this library instead of Redux or Mobx. also you can access state from all of your components and behavior with them using hooks.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
