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

react-use-observe-changes

v13.0.3

Published

The useObserveChanges is a custom React hook that observes changes in specific fields and updates the observed state. It is useful for tracking and managing the state of multiple fields in a React component efficiently.

Readme

useObserveChanges

npm version License: MIT Build Status Downloads

The useObserveChanges is a custom React hook that observes changes in specific fields and updates the associated state. It is particularly useful for tracking and managing the state of multiple fields efficiently within a React component.


Features

  • Dynamic State Tracking: Tracks specific fields dynamically across multiple instances.
  • Lightweight: Efficiently handles state with minimal overhead.
  • Flexible API: Supports observing, unobserving, and resetting fields dynamically.

Installation

Install the package via npm:

npm install react-use-observe-changes

Usage

To use useObserveChanges, import the hook into your component:

Basic Example

import React from 'react';
import useObserveChanges from 'react-use-observe-changes';

const MyComponent = () => {
    const { observeFieldOf, getInstance } = useObserveChanges();

    const handleFirstnameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
        observeFieldOf('person', 'firstName', e.target.value);
    };

    const handleLastnameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
        observeFieldOf('person', 'lastName', e.target.value);
    };

    return (
        <div>
            <input name="firstName" onChange={handleFirstnameChange} placeholder="First Name" />
            <input name="lastName" onChange={handleLastnameChange} placeholder="Last Name" />
            <pre>{JSON.stringify(getInstance('person'), null, 2)}</pre>
        </div>
    );
};

export default MyComponent;

API

useObserveChanges(logLevelDesc?: string)

A hook that observes changes in specific fields and updates the associated state.

Methods:

  • getInstance(instance: string): object | undefined Retrieves the state of a registered instance.

  • observeFieldOf(instance: string, field: string, value: any): void Observes changes in a specific field of an instance.

  • unobserveFieldOf(instance: string, field: string): void Stops observing a specific field of an instance.

  • reset(): void Resets the state of all observed instances.


Examples

Observing Changes in a Field

const { observeFieldOf, getInstance } = useObserveChanges();

// Observe changes in a specific field
observeFieldOf('user', 'name', 'John');

// Retrieve the state of the instance
console.log(getInstance('user')); // { name: 'John' }

Removing an Observed Field

const { observeFieldOf, unobserveFieldOf, getInstance } = useObserveChanges();

observeFieldOf('user', 'name', 'John');
unobserveFieldOf('user', 'name');

console.log(getInstance('user')); // {}

Resetting the State

const { observeFieldOf, reset, getInstance } = useObserveChanges();

observeFieldOf('user', 'name', 'John');
reset();

console.log(getInstance('user')); // undefined

Visual Overview

What is useObserveChanges?

What is useObserveChanges

Core Features

Core Features

How to Use

How to Use


Notes

  • The state is stored in memory and persists as long as the component using the hook is mounted.
  • The hook supports multiple instances and multiple fields per instance.
  • There is no explicit limit to the number of instances or fields, but excessive use may impact performance.

Contribution

Contributions are welcome! Follow these steps to contribute:

  1. Fork the repository.
  2. Create a new branch for your feature (git checkout -b feature-name).
  3. Commit your changes (git commit -m "Add new feature").
  4. Push the branch (git push origin feature-name).
  5. Open a pull request.

License

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


Learn More

Visit the GitHub Pages for a detailed overview and documentation.