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

usegs

v1.0.2

Published

A simple global state management library for React using useGS.

Downloads

5

Readme

usegs

usegs is a simple and efficient global state management library for React, using the useGS hook.

Installation

To install usegs, use npm or yarn:

npm install usegs

or

yarn add usegs

Getting Started

Initializing Global State

To start using usegs, initialize your global state with the initGS function. This function takes an object where each key-value pair represents the initial state and its default value.

import { initGS } from "usegs";

// Initialize global state with default values
initGS({
  CURRENT_FOLDER: { items: { customItem: true } },
  SHOW_PAYMENT_GUIDE: true,
});

Using Global State in React Components

The useGS hook allows you to access and update global state within your React components.

import React from "react";
import { useGS } from "usegs";

function MyComponent() {
  // Use the useGS hook to get the current state and the function to update it
  const [currentFolder, setCurrentFolder] = useGS("CURRENT_FOLDER");

  // Example function to update the state
  const addItem = () => {
    setCurrentFolder({ items: { newItem: true } });
  };

  return (
    <div>
      <h1>Current Folder</h1>
      <pre>{JSON.stringify(currentFolder, null, 2)}</pre>
      <button onClick={addItem}>Add Item</button>
    </div>
  );
}

export default MyComponent;

Managing Global State in JavaScript Files

usegs also provides functions getGS and setGS to interact with global state outside of React components, such as in utility functions or event handlers.

import { getGS, setGS } from "usegs";

// Get the current value of a global state
const folderState = getGS("CURRENT_FOLDER");
console.log("Current Folder State:", folderState);

// Update the value of a global state
setGS("CURRENT_FOLDER", { items: { anotherItem: true } });

API Reference

initGS(initialState)

  • Description: Initializes the global state with the provided object. Each key-value pair sets the initial state for a specific key.
  • Parameters:
    • initialState (Object) - An object containing initial state values.
  • Example:
initGS({
  CURRENT_FOLDER: { items: {} },
  SHOW_PAYMENT_GUIDE: false,
});

useGS(key, initialValue)

  • Description: React hook to use and update a global state within a component.
  • Parameters:
    • key (string) - The key of the global state.
    • initialValue (any) - Optional initial value if the state key is not already registered.
  • Returns: [stateValue, setStateFunction]
  • Example:
const [currentFolder, setCurrentFolder] = useGS("CURRENT_FOLDER", {
  items: {},
});

getGS(key)

  • Description: Retrieves the value of a global state.
  • Parameters:
    • key (string) - The key of the global state to retrieve.
  • Returns: The current value of the specified global state, or null if the key is not found.
  • Example:
const folderState = getGS("CURRENT_FOLDER");

setGS(key, newValue)

  • Description: Updates the value of a global state and notifies all registered listeners.
  • Parameters:
    • key (string) - The key of the global state to update.
    • newValue (any) - The new value to set for the state.
  • Example:
setGS("CURRENT_FOLDER", { items: { updatedItem: true } });

License

This project is licensed under the MIT License - see the LICENSE file for details.