npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-redux-global-state-manager

v1.0.4

Published

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.

Downloads

15

Readme

react-redux-global-state-manager

logo

Work on both React and React-Native

Fully native global state manager using react Hooks (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 use Redux with Reducer and Actions 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.js
store/storeActions.js
store/storeNames.js

Step 1: Create new store (Redux like)

Create each store separately in store/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.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 Types
const types = {
  SET_TOKEN_INFO: "SET_TOKEN_INFO",
};
// define store with name,initialState and reducer
const token = createStore(
  "token", // unique name for this token store
  {        // initialize state object
    token: null,
    public_key: "",
    expire_in: "",
    login_time: ""
  }, 
  (state, action) => { // define reducer
    // when a reducer is being used, you must return a new state value
    switch (action.type) {
      case types.SET_TOKEN_INFO:
        return {
          ...state,
          ...action.payload
        };
      default:
        return state;
    }
  }
);

export { token, types }; //export token reference and types

Step 2: Create action file

Create separate action file for each store in store/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.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";

const token = {
  setTokenInfo: data => {
    return {
      type: types.SET_TOKEN_INFO,
      payload: data
    };
  }
};

export { token };

Note: follow the files format similar to others files inside actions and stores directories.

Step 3: Add exported stores and actions respectively to storeNames.js and storeActions.js

e.g: add store/stores/token.store.js into store/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 app
const storeNames = {
  token,
  profileInfo,
};
// ------
export { storeNames }; // export this object as container for all of our stores.

e.g: add store/actions/token.action.js into store/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 app
const storeActions = {
  token,
  profileInfo,
};

// ------
export { storeActions }; // export this object as container for all of our Actions.

Step 4: Create one EntryPoint for your store

Create store/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 references
import { storeActions } from "./storeActions.js"; // All actions references

// ------
export { storeNames, storeActions, useStore, readOnlyStore, dispatchDirectly };

Final Step:

Call store/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 reference
  storeActions, // access all actions functions
  useStore, // hook - use this in functional component and return [state,dispatch]
  readOnlyStore, // just return current store value in non-component functions
  dispatchDirectly // To access dispatch in non-component functions
} from "../store/index";

For use in functional 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 function
  const data={
    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 destructuring 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 to read data from store or dispatch some data in it, you can use this approach.

Read:

//read whole token object out of component function, using store name reference
const token=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

|Function |Description |Return value | |--------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|---------------------------------| |useStore(storeReference) | This function take your store reference to access the current state and dispatch function. Each time the state of the current store going to change, every component that used useStore() will be re-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 |

Please subscribe and contribute with me to develop this lib


Notice:

This library inspired by https://github.com/jhonnymichel/react-hookstore