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

nr-usestate

v1.0.1

Published

To install dependencies:

Readme

nr-usestate: A Simple State Management Utility

This repository contains a simple state management utility implemented in JavaScript (with corresponding TypeScript definition files) that is similar to React's useState hook. It can be used for managing state in small, self-contained applications or pure JavaScript environments.

Features

  • Simple State Storage: Store any JavaScript value (numbers, strings, objects, etc.).
  • Value Updates: Update the state with a direct value or a function based on the previous state.
  • Side Effect Management (useEffect): Run a specified function when the state changes or when useEffect is registered. Crucially, useEffect callbacks are executed immediately upon registration with the current state.
  • Access Methods: Easy access to the state via the value property, toString(), and valueOf() methods.
  • Lightweight and Standalone: Has no external dependencies.
  • TypeScript Support: Includes a type definition file (.d.ts) for TypeScript projects.
  • Flexibility: Choose between class-based or functional approaches.

Project Structure

node-usestate/
├── index.js          # Main JavaScript utility file (Class-based or Functional)
├── index.d.ts        # TypeScript definition file for the utility
├── examples/
│   └── index.js      # Example usage demonstrating the utility
└── README.md         # This file

Usage Example (examples/index.js):

// examples/index.js
import { useState } from "../index.js";
const counter = new useState(0);

console.log("Initial value:", counter.value);

counter.useEffect((currentCount) => {
  console.log("useEffect triggered (Counter):", currentCount);
});

counter.set(5);

counter.set((prev) => prev + 1);

console.log("Current value:", counter.value);

const greeting = new useState("Hello");

greeting.useEffect((currentText) => {
  console.log(
    "useEffect triggered (Greeting):",
    currentText.toUpperCase() + "!"
  );
});

greeting.set("World");

2. Functional Approach (If you prefer, configure your index.js file this way)

export function useState(initialState) {
  let state = initialState;
  const _effects = [];

  const stateObject = {
    useEffect: function (callback) {
      _effects.push(callback);
      callback(state);
    },
    // ...
  };
  return stateObject;
}

Usage Example (examples/index.js):

// examples/index.js
import { useState } from "../index.js";

const counter = useState(0);

console.log("Initial value:", counter.value);

counter.useEffect((currentCount) => {
  console.log("useEffect triggered (Counter):", currentCount);
});

counter.set(5);

counter.set((prev) => prev + 1);

console.log("Current value:", counter.value);

API Reference

Both approaches (class-based and functional) essentially expose the same API:

useState<T>(initialState: T)

  • Parameters:
    • initialState: T: The initial value of the state. T can be any JavaScript type.
  • Returns: An object to manage the state.

Properties and Methods of the Returned Object:

  • value: T (getter)
    • Returns the current value of the state. It's a read-only property.
  • set(newValue: T | ((_state: T) => T)): this
    • Updates the state's value.
    • Parameters:
      • newValue: The new value for the state, OR a function that receives the current state and returns the new state.
    • Returns: The current useState object for chaining.
  • useEffect(callback: (_state: T) => any): void
    • Registers a callback function to be invoked whenever the state is updated.
    • Important: It is also triggered once immediately upon registration with the current state value.
    • Parameters:
      • callback: A function that receives the new state as an argument.
  • toString(): string
    • Returns the string representation of the state.
  • valueOf(): T
    • Returns the primitive value of the state (useful if the state is an object).

TypeScript Support

The index.d.ts file is included to provide proper type checking and autocompletion when using this utility in your TypeScript projects.

import { useState } from "node-usestate";

const count = new useState<number>(0);
count.useEffect((c) => {
  console.log("Counter changed:", c);
});

const user = useState<{ name: string; age: number }>({
  name: "Alice",
  age: 30,
});
user.set((prev) => ({ ...prev, age: prev.age + 1 }));

Contributing

Contributions and bug fixes are welcome! Feel free to open a pull request or an issue.