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-global-state-manager

v0.3.4

Published

Global state manager for ReactJS using React Hooks API

Downloads

8

Readme

Global State Manager

Create global state manager utilizing React Hooks API. (No Redux!)

Install

  • npm install react-global-state-manager

Usage

GlobalStateManager

Use GlobalStateManager to initiate global state manger for your app.

import React from 'react';
import ReactDOM from 'react-dom';
import { GlobalStateManager } from 'react-global-state-manager';

const App = () => {
    // Create Global State Manager 
    const state = GlobalStateManager({ apple: 5, orange: 10 });
    return (
    <>
        <h1>Fruit Vendor</h1>
        <table>
            <tr>
                <th>Fruit</th>
                <th>Cost</th>
            </tr>
            <tr>
                <td>Apple</td>
                <td>{state.apple}</td>
            </tr>
            <tr>
                <td>Orange</td>
                <td>{state.orange}</td>
            </tr>
        </table>
    </> 
    );
}

ReactDOM.render(
  <App />,
  document.getElementById('root')
);

useGlobalState

useGlobalState allows you to grab specific state in GSM and subscribe your container to listen to the changes to the requested state.

...

const Apple = () => {
  const price = useGlobalState('apple');

  return (
    <tr>
      <td>Apple</td>
      <td>{ price }</td>
    </tr>
  )
}

const Orange = () => {
  const price = useGlobalState('orange');

  return (
    <tr>
      <td>Orange</td>
      <td>{ price }</td>
    </tr>
  )
}

const Fruit = () => {
  const state = GlobalStateManager({ apple: 5, orange: 10 });
  return (
    <>
      <h1>Fruit Vendor</h1>
      <table>
        <tr>
          <th>Fruite</th>
          <th>Cost</th>
        </tr>
        <tbody>
          <Apple />
        </tbody>
        <tbody>
          <Orange />
        </tbody>
      </table>
    </>
  )
}

...

addAction

Create a new action which will trigger state update.

...

const Fruit = () => {
  const state = GlobalStateManager({ apple: 5, orange: 10 });

  addAction('changeApple', (state, newPrice) => {
    return { apple: newPrice };
  });

  return (
    <>
      <h1>Fruit Vendor</h1>
      <table>
        <tr>
          <th>Fruite</th>
          <th>Cost</th>
        </tr>
        <tbody>
          <Apple />
        </tbody>
        <tbody>
          <Orange />
        </tbody>
      </table>
    </>
  )
}

...

useAction

Get a named action from GSM. Performing this action will trigger all components subscribed to this state to be re-rendered.

...

const Apple = () => {
  const [newPrice, setNewPrice] = useState('');
  const price = useGlobalState('apple');
  const changePrice = getAction('changeOrange');
  return (
    <>
      <tr>
        <td>Apple</td>
        <td>{ price }</td>
      </tr>
      <div style={{'position': 'aboslute', 'right': '0'}}>
        <input type='text' value={newPrice} placeholder='New Price' onChange={(e) => setNewPrice(e.target.value)} />
        <button onClick={()=>changePrice(parseInt(newPrice))}>Change</button>
      </div>
    </>
  )
}

const Orange = () => {
  const [newPrice, setNewPrice] = useState('');
  const price = useGlobalState('orange');
  const changePrice = getAction('changeApple');

  return (
    <>
      <tr>
        <td>Orange</td>
        <td>{ price }</td>
      </tr>
      <div style={{'position': 'aboslute', 'right': '0'}}>
        <input type='text' value={newPrice} placeholder='New Price' onChange={(e) => setNewPrice(e.target.value)} />
        <button onClick={()=>changePrice(parseInt(newPrice))}>Change</button>
      </div>
    </>
  )
}

const Fruit = () => {
  const state = GlobalStateManager({ apple: 5, orange: 10 });

  addAction('changeApple', (state, newPrice) => {
    return { apple: newPrice };
  });

  addAction('changeOrange', (state, newPrice) => {
    return { orange: newPrice };
  });

  return (
    <>
      <h1>Fruit Vendor</h1>
      <table>
        <tr>
          <th>Fruite</th>
          <th>Cost</th>
        </tr>
        <tbody>
          <Apple />
        </tbody>
        <tbody>
          <Orange />
        </tbody>
      </table>
    </>
  )
}

...

In this example, apple vendor can change the price of an orange and orange vendor can change the price of an apple.

addSideEffect

Introduce side effect to be ran when desired state is being updated.

  ...
  
  const Fruit = () => {
    const state = GlobalStateManager({ apple: 5, orange: 10 });
    
    ...
    // set side effect for apple.
    addSideEffect((state) => {
      console.log('You can't mess with me! I add 5 more!');
      return { apple: state.apple + 5}
    }, 'apple');
    
    // set side effect for multiple states.
    addSideEffect((state) => {
      console.log('Price changed in fruits');
      return state
    }, 'apple', 'orange');
    
    ...
    
  }
  • Top effect will increase price of an apple by 5 whenever orange vendor tries to change the price of an apple.
  • You can also set one side effect for multiple states.

addState (WIP)

In progress. addState will add a state from anywhere in your app, and will subscribe current container to listen to the changes. You can only use newly added state from children of current container. If you try to useGlobalState from any other container, it will throw an error.