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

origami-state-manager

v1.0.5

Published

A simple react state management library.

Downloads

5,541

Readme

Origami-State-Manager (OSM)

Origami-State-Manager (OSM), pronounced "awesome," is a lightweight state management library that requires minimal boilerplate. OSM stands out for its ability to seamlessly access and update global states from both React and non-React environments.

Why the Name “Origami”?

Origami is the Japanese art of paper folding, known for transforming a simple sheet into intricate designs. Just like Origami, OSM is flexible, scalable, and adaptable, allowing you to effortlessly shape your global state. And, just like paper, it’s incredibly lightweight.

⚠️ Disclaimer: OSM is in its early stages and hasn’t been fully tested. Be cautious as you might encounter some Oopsies along the way.


Installation

Ready to add some OSM-ness to your project? Install the library using npm:

npm install origami-state-manager

Creating a Store

A store is where all OSM states are stored. To create a store, pass the initial states to the createStore method and export the newly created store:

// store.ts
import { createStore } from "origami-state-manager";

const initialValues = {
  origami: 0,
  osmness: 0,
};

export const store = createStore(initialValues);

Making the Store Persistent

By default, the store is not persistent. To make a store persistent, pass a store name as the second argument to the createStore method:

export const store = createStore(initialValues, "myOSMness");

Usage

React Hook: useStateListener

This hook allows you to access and listen to state changes within a React component:

import { useStateListener } from "origami-state-manager";
import { store } from "./store";

function ExampleComponent() {
  const origami = useStateListener("origami", store);

  return <div>Origami Count: {origami}</div>;
}

The hook takes two arguments: the state name and the store instance.

Accessing State Properties

Each state provides the following methods:

  • value: Current value.
  • subscribe: Registers a callback function that triggers when the state changes. The useStateListener hook uses this method internally.

Example:

import { useEffect, useState } from "react";
import { store } from "./store";

function ExampleComponent() {
  const [origamiValue, setOrigamiValue] = useState(store["origami"].value);

  useEffect(() => {
    const unsubscribe = store["origami"].subscribe((newState) => {
      setOrigamiValue(newState);
    });
    return unsubscribe;
  }, []);

  return <div>Origami Value: {origamiValue}</div>;
}

Examples: Accessing & Updating State

Updating a State

To update a state, assign a new value to the value property:

import { store } from "./store";

function UpdateOrigami() {
  return (
    <button onClick={() => (store["origami"].value = new Date().getSeconds())}>
      Update Origami
    </button>
  );
}

Accessing State in a React Component

Use the useStateListener hook to access state values in another component:

import { store } from "./store";
import { useStateListener } from "origami-state-manager";

function DisplayCounts() {
  const origami = useStateListener("origami", store);
  const osmness = useStateListener("osmness", store);

  return (
    <>
      <h1>OSM-ness Count: {osmness}</h1>
      <h2>Origami Count: {origami}</h2>
    </>
  );
}

Accessing and Updating State from Non-React Files

You can access and update state values from non-React files or functions as well:

// utils.js

function getUserProfile() {
  let profile = store["profile"].value;
  if (!profile) {
    store["profile"].value = {};
  }

  return store["profile"].value;
}

Background

The development of the OSM library began as a research project aimed at creating a lightweight global state management solution with minimal boilerplate. The goal was to enable seamless state updates and access across both React and non-React functions while keeping the setup simple, easy, and lightweight.

Given that OSM is still in its early stages, it is best suited for smaller projects or applications with straightforward state management needs.


Contributing to Origami-State-Manager

Contributions are welcome! Check out the contribution guidelines to learn more.


Changelog

Stay up-to-date with the latest changes by visiting the changelog, which is regularly updated with new releases.