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 🙏

© 2026 – Pkg Stats / Ryan Hefner

one-global-state

v1.3.0

Published

Lightweight React global state hook — useGlobalState()

Readme

one-global-state

A tiny React hook that gives you useState-style API with automatic global sync across all components — no Context, no Provider, no prop drilling.

npm license

Installation

npm install one-global-state

Quick start

import { useGlobalState } from "one-global-state";

function Counter() {
  const [count, setCount] = useGlobalState("count", 0);
  return <button onClick={() => setCount((n) => n + 1)}>Count: {count}</button>;
}

Any component that calls useGlobalState with the same key will share the same value and re-render whenever it changes.

API

useGlobalState<T>(key, initialValue)

const [value, setValue] = useGlobalState<T>(key: string, initialValue: T)

| Param | Type | Description | | -------------- | -------- | --------------------------------------------- | | key | string | Unique identifier for the global state entry | | initialValue | T | Used only if no value exists for this key yet |

Returns [value, setValue] — identical shape to useState.

setValue accepts both a direct value and a functional updater:

setValue(42); // direct value
setValue((prev) => prev + 1); // functional updater

setGlobalState<T>(key, value) — standalone setter

Update global state from outside a React component (event handlers, utils, etc.):

import { setGlobalState } from "one-global-state";

setGlobalState("count", 0); // direct value
setGlobalState("count", (n) => n + 1); // functional updater

Usage examples

Multiple components sharing one key

function CounterDisplay() {
  const [count] = useGlobalState("count", 0);
  return <span>{count}</span>;
}

function CounterControls() {
  const [, setCount] = useGlobalState("count", 0);
  return (
    <>
      <button onClick={() => setCount((n) => n - 1)}>−</button>
      <button onClick={() => setCount((n) => n + 1)}>+</button>
      <button onClick={() => setCount(0)}>Reset</button>
    </>
  );
}

Both components stay in sync automatically — no shared parent state needed.


Multiple independent keys

function Profile() {
  const [name, setName] = useGlobalState("username", "");
  const [theme, setTheme] = useGlobalState<"light" | "dark">("theme", "light");

  return (
    <>
      <input value={name} onChange={(e) => setName(e.target.value)} />
      <button
        onClick={() => setTheme((t) => (t === "light" ? "dark" : "light"))}
      >
        Toggle theme
      </button>
    </>
  );
}

Array state with functional updater

type Todo = { id: number; text: string; done: boolean };

function useTodos() {
  return useGlobalState<Todo[]>("todos", []);
}

function AddTodo() {
  const [, setTodos] = useTodos();
  const add = (text: string) =>
    setTodos((prev) => [...prev, { id: Date.now(), text, done: false }]);
  // ...
}

function TodoList() {
  const [todos, setTodos] = useTodos();
  const toggle = (id: number) =>
    setTodos((prev) =>
      prev.map((t) => (t.id === id ? { ...t, done: !t.done } : t)),
    );
  // ...
}

Calling setGlobalState outside React

// Reset all state on logout — no hook needed
function logout() {
  setGlobalState("username", "");
  setGlobalState("todos", []);
}

Production Use Case

to avoid dealing with all unique keys management, it will be better to wrap each global state inside a custom hook.

For example, if we want to store a global filter for a table which is displaying the data as per the filter set, we can use something like below

//GLOBAL CONSTS
const FILTER_KEY = "filter-key";

function useGlobalFilter() {
  return useGlobalState<FilterSettings>(FILTER_KEY, {
    order: "desc",
    sort: "name",
  });
}

Then use the useGlobalFilter where ever required inside the project.

How it works

  • A module-level Map stores values keyed by string — shared across the entire app.
  • Built on useSyncExternalStore, React's purpose-built API for external stores, ensuring tear-free re-renders under concurrent mode.
  • React is a peer dependency — the library ships zero runtime dependencies.

Requirements

  • React ≥ 17 (uses useSyncExternalStore via React 18+; for React 17 add the shim)

License

MIT