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 🙏

© 2025 – Pkg Stats / Ryan Hefner

nexus-state

v3.1.0

Published

Lightweight global state manager for React and JavaScript.

Downloads

33

Readme

nexus-state logo

Table of contents

About

Lightweight, framework-agnostic state management with optional actions and React bindings. Designed for simplicity and performance with TypeScript support.

Installation

npm install nexus-state

Configuration

The library provides two separate builds:

  • **Modern bundlers*use the **ESM*(import) build
  • **Node.js*use the **CommonJS*(require) build

Import the entire API via the default export or as individual named imports:

import NXS from "nexus-state";
// or
import { createNexus, createReactNexus, createActs } from "nexus-state";

API

main:

import { createNexus } from "nexus-state";

const nexus = createNexus({
  state: {
    count1: 0,
    count2: 0,
  },

  acts: (get, set) => ({
    increment() {
      set((state) => ({ count1: state.count1 + 1 }));
      this.getState("count1"); // ! calling another action
    },
    getState(value) {
      console.log(`${value}:`, get(value));
    },
  }),
});

export default nexus;
type MyStateT = {
  count1: number;
  count2: number;
};

type MyActionsT = {
  increment: () => void;
  consoleCalling: (text: string) => void;
};

const nexus = createNexus<MyStateT, MyActionsT>({...});
import { createReactNexus } from "nexus-state";

const nexus = createReactNexus({
  state: {
    count1: 0,
    count2: 0,
  },

  acts: (get, set) => ({
    increment() {
      set((state) => ({ count1: state.count1 + 1 }));
      this.getState("count1"); // ! calling another action
    },
    getState(value) {
      console.log(`${value}:`, get(value));
    },
  }),
});

export default nexus;
type MyStateT = {
  count1: number;
  count2: number;
};

type MyActionsT = {
  increment: () => void;
  consoleCalling: (text: string) => void;
};

const nexus = createReactNexus<MyStateT, MyActionsT>({...});
import { ✦create, createActs } from "nexus-state";

const customActions = createActs((get, set) => ({
  increment() {
    set((state) => ({ count1: state.count1 + 1 }));
    this.getState("count1"); // ! calling another action
  },
  getState(value) {
    console.log(`${value}:`, get(value));
  },
}));

// Usage:
const nexus = ✦create({
  state: {...},
  acts: customActions, // ! supports multiple: [myActions, myAnotherActions]
});

export default nexus;

// ✦create - createNexus or createReactNexus
type MyStateT = {...};
type MyActionsT = {...};

const customActions = createActs<MyStateT, MyActionsT>(...);

// ✦ Note:
// use optional chaining (?) when calling other actions via "this"
const incrementAction = createActs<MyStateT, MyActionsT>(() => ({
  increment() {
    this.getState?.("count1"); // ?.
  },
}));
import { ✦create } from "nexus-state";

const nexus1 = ✦create({...});
const nexus2 = ✦create({...});

// ✦create - createNexus or createReactNexus

nexus:

import nexus from "your-nexus-config";

const entireState = nexus.get();
const specificValue = nexus.get("key");
import nexus from "your-nexus-config";

// Direct update:
nexus.set({ count1: 5 });
nexus.set({ count1: 5, count2: 10 }); // multiple

// Functional update:
nexus.set((state) => ({
  count1: state.count1 + 1,
}));

// With context for `middleware`
set({ key: newValue }, { source: "server", meta: { ... } });

// Shortcut equivalent to { source: "server" }
set({ key: newValue }, "server");
import nexus from "your-nexus-config";

nexus.reset(); // reset entire state
nexus.reset("count1", "count2");
import nexus from "your-nexus-config";

const unsubscribe = nexus.subscribe(
  // observer:
  (state) => {
    console.log("count1 changed:", state.count1);
  },
  // dependencies:
  ["count1"]
);

// Unsubscribe
unsubscribe();
import nexus from "your-nexus-config";

nexus.middleware((prev, next, context) => {
  if (context.source === "storage") {
    console.log("Loaded from storage:", next);
  }
  // You can return a modified state or perform side effects
  return next;
});
import nexus from "your-nexus-config";

// Setup Redux DevTools connection
const devtools = window.__REDUX_DEVTOOLS_EXTENSION__?.connect({
  name: "MyStore",
});

devtools?.init(nexus.get());

// Register middleware to send state updates to DevTools
nexus.middleware((_, next) => {
  devtools?.send?.({ type: "UPDATE" }, next);
});
interface ReduxDevToolsConnection {
  send: (action: unknown, state: unknown) => void;
  init: (state: unknown) => void;
}

interface ReduxDevToolsExtension {
  connect(options: { name: string }): ReduxDevToolsConnection;
}

declare global {
  interface Window {
    __REDUX_DEVTOOLS_EXTENSION__?: ReduxDevToolsExtension;
  }
}

Description: object containing custom actions. Usage Example:

import nexus from "your-nexus-config";

nexus.acts.increment();
nexus.acts.consoleCalling("Some text");
// regular function
increment() {
  this.consoleCalling("Increment called"); // working
}

// arrow function
increment: () => this.consoleCalling("Increment called") // not working
// but syntax is compacter

More info: Arrow Functions

import nexus from "your-nexus-config";

const entireState = nexus.use();
const specificValue = nexus.use("key");

✦ Note: Unlike get, *usetriggers a re-render when the state changes.

import nexus from "your-nexus-config";

const total = nexus.useSelector(
  // observer:
  (state) => state.count1 + state.count2,
  // dependencies:
  ["count1", "count2"]
);
import { useCallback } from "react";
import nexus from "your-nexus-config";

const total = nexus.useSelector(
  useCallback((state) => state.count1 + state.count2, []),
  ["count1", "count2"]
);
import nexus from "your-nexus-config";

const updater = nexus.useRerender();
updater(); // force re-render

License

MIT