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

@anikghosh256/reactive-state

v1.0.0

Published

A lightweight reactive state management library with deep reactivity using Proxy

Readme

@anikghosh256/reactive-state

A lightweight, reactive state management library with deep reactivity using Proxy. It is ideal for managing application state with automatic updates to subscribed listeners, making it easy to build reactive applications.

Features

  • Deep Reactivity: Automatically creates deeply reactive objects using Proxy, so nested objects or arrays also trigger reactivity.
  • Automatic Updates: Subscribed listeners are automatically notified when the state changes.
  • Simple API: Easy-to-use methods for managing state and triggering re-renders.

Installation

To install @anikghosh256/reactive-state, use npm or yarn:

npm install @anikghosh256/reactive-state

Usage

Creating a Reactive State

Use the createReactiveState function to create a reactive state. Pass an initial state object, and it returns an object with the reactive state and methods for managing it.

const { createReactiveState } = require("@anikghosh256/reactive-state");

// Initial state
const initialState = {
  user: {
    name: "John",
    age: 30,
  },
  counter: 0,
};

// Create reactive state
const stateManager = createReactiveState(initialState);

// Accessing the reactive state
console.log(stateManager.state.user.name); // John
console.log(stateManager.state.counter); // 0

Subscribing to State Changes

To listen for changes to the state, use the onChange method. This method takes a listener function that is called whenever the state is updated.

// Subscribe to state changes
const unsubscribe = stateManager.onChange(() => {
  console.log("State updated:", stateManager.state);
});

// Trigger a state change
stateManager.setState({ counter: 1 }); // This will trigger the listener

// Unsubscribe from state changes
unsubscribe();

Updating the State

Use the setState method to update parts of the state. This method takes an object with the updated state values. If any values have changed, all subscribed listeners are notified.

// Update state and trigger listeners
stateManager.setState({ user: { name: "Alice" } }); // This will trigger the listener

// Optional: Prevent automatic notification of listeners by passing false as the second argument
stateManager.setState({ counter: 2 }, false); // This will not trigger the listener

Unsubscribing from State Changes

To stop listening for state updates, call the unsubscribe function that onChange returns. This prevents the listener from being triggered when the state changes.

// Subscribe to state updates
const unsubscribe = stateManager.onChange(() => {
  console.log("State updated:", stateManager.state);
});

// Unsubscribe from state updates
unsubscribe();

// State changes will no longer trigger the listener
stateManager.setState({ counter: 3 });

Full Example

Here’s a full example demonstrating how to create reactive state, subscribe to changes, update the state, and unsubscribe from changes:

const { createReactiveState } = require("@anikghosh256/reactive-state");

const initialState = {
  user: {
    name: "John",
    age: 30,
  },
  counter: 0,
};

// Create reactive state
const stateManager = createReactiveState(initialState);

// Subscribe to state changes
const unsubscribe = stateManager.onChange(() => {
  console.log("State updated:", stateManager.state);
});

// Trigger a state update
stateManager.setState({ user: { name: "Alice" } }); // State updated: { user: { name: 'Alice', age: 30 }, counter: 0 }

// Update counter
stateManager.setState({ counter: 1 }); // State updated: { user: { name: 'Alice', age: 30 }, counter: 1 }

// Prevent re-render by passing false
stateManager.setState({ counter: 2 }, false); // State updated: { user: { name: 'Alice', age: 30 }, counter: 2 }

// Unsubscribe from state changes
unsubscribe();

// This change will NOT trigger the listener
stateManager.setState({ counter: 3 }); // (No output)

API Reference

createReactiveState(initialState)

  • Parameters:
    • initialState (Object) - The initial state object to create the reactive state.
  • Returns:
    • An object with the following methods:
      • state (Object) - The deeply reactive state object.
      • onChange(listener) (Function) - Subscribes to state changes. Returns an unsubscribe function.
      • setState(updates, reRender) (Function) - Updates the state. If reRender is true (default), listeners are notified. Set reRender to false to prevent notification.

onChange(listener)

  • Parameters:
    • listener (Function) - A callback function that is called when the state changes.
  • Returns:
    • A function to unsubscribe from state updates.

setState(updates, reRender)

  • Parameters:
    • updates (Object) - The new values to update in the state.
    • reRender (Boolean) - Optional. Defaults to true. If set to false, listeners won't be notified of the change.
  • Returns:
    • void.

unsubscribe()

  • A function returned by onChange that stops further notifications for state updates.

License

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