Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

NotificationsYou must be signed in to change notification settings

intellidev1991/react-redux-global-state-manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logo

Work on bothReact andReact-Native

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.

Example:

Edit ecstatic-chatterjee-jvyd2

Installation

yarn

yarn add react-redux-global-state-manager

npm

npm i react-redux-global-state-manager

My recommended Pattern

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)

Recommended Directory Structure:

Create these directories structure in your app for state management:

store/actions/store/stores/store/index.jsstore/storeActions.jsstore/storeNames.js

Step 1: Create new store (Redux like)

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 insidestoreNames.js for 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

Step 2: Create action file

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 insidestoreActions.js for 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 insideactions andstores directories.

Step 3: Add exportedstores andactions respectively tostoreNames.js andstoreActions.js

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.

Step 4: Create one EntryPoint for your store

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};

Final Step:

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

Using store in our components

Import required functions from entry point at/store/index.js

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";

For use infunctional Components:

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 usedestructuring properties:

const[{token,public_key,expire_in},dispatch]=useStore(storeNames.token);

For use in regular functions (None-Component functions):

If in a situation you (had to)forced toread data from store ordispatch some data in it, you can use this approach.

Read:

//read whole token object out of component function, using store name referenceconsttoken=readOnlyStore(storeNames.token);

Write:

//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));

Functions

FunctionDescriptionReturn 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 futureReference to this store
readOnlyStore(storeReference)Just return current store value in non-component functionsCurrent state of specific store
dispatchDirectly(storeReference,action)To access dispatch in non-component functionsnull

Please subscribe and contribute with me to develop this lib


Notice:

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp