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-changes-listener

v1.0.2

Published

A React utility component to listen for changes in a value.

Readme

Description

The ChangesListener component is a utility for observing changes in a reactive value within your React application. It leverages a custom hook (useValue) to access a dynamic value and triggers a provided listener function whenever this value changes. Importantly, ChangesListener itself does not render any DOM elements, making it a purely functional component for side-effect management based on value changes.

Key Features:

  • Change Detection: It efficiently detects changes in the value returned by the useValue hook.
  • Targeted Listening (Optional): For object types, you can specify a field prop to listen for changes in a specific property instead of the entire object.
  • Non-Rendering: It doesn't add any extra nodes to your component tree.
  • Clear Separation of Concerns: It isolates the logic for reacting to value changes, promoting cleaner and more maintainable components.

Use Cases:

  • Performing actions when a specific piece of state updates.
  • Triggering animations or transitions based on data changes.
  • Synchronizing data with external systems or APIs when a relevant value is modified.
  • Implementing custom logging or analytics based on state transitions.

How it Works:

  1. It accepts a useValue prop, which should be a function (typically a custom hook) that returns the reactive value you want to observe.
  2. It uses the provided useValue hook to get the current value.
  3. It optionally accepts a field prop. If provided and the value is an object, it will only track changes to the property specified by this field.
  4. It utilizes the usePrevious hook to store the previous value.
  5. In each render, it compares the current value (or the specific property if a field is provided) with the previous value.
  6. If a change is detected, it calls the listener function in an effect after the render commit, passing the new value (or the new value of the specific property).

Design Rationale:

  • The listener runs in an effect to keep side effects out of the render phase.
  • This avoids StrictMode double-calls that can happen when doing work in render.
  • Running after commit is safer for side-effectful callbacks (logging, analytics, network calls) and still keeps the component renderless.
  • The field prop only applies to object values; omit it when watching primitives.

Example Usage:

Watching for changes in a primitive value:

import { useState } from 'react';
import { ChangesListener } from 'react-changes-listener'

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <>
      <button onClick={() => setCount(c => c + 1)}>Increment</button>
      <ChangesListener
        useValue={() => count}
        listener={(newCount) => console.log('Count changed to', newCount)}
      />
    </>
  );
}

Watching for changes in a state value:

import { useState } from 'react';
import { ChangesListener } from 'react-changes-listener'

function MyComponent() {
  const [count, setCount] = useState(0);

  const handleCountChange = (newCount: number) => {
    console.log(`Count changed to: ${newCount}`);
    // Perform other actions based on the new count
  };

  const useCurrentCount = () => count;

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(c => c + 1)}>Increment</button>
      <ChangesListener listener={handleCountChange} useValue={useCurrentCount} />
    </div>
  );
}

Watching for changes in a specific property of an object:

import { useState } from 'react';
import { ChangesListener } from 'react-changes-listener'

interface User {
  name: string;
  age: number;
}

function UserProfile() {
  const [user, setUser] = useState<User>({ name: 'Alice', age: 30 });

  const handleNameChange = (newName: string) => {
    console.log(`Name changed to: ${newName}`);
    // Update user profile in the backend, etc.
  };

  const useCurrentUser = () => user;

  return (
    <div>
      <p>Name: {user.name}</p>
      <p>Age: {user.age}</p>
      <button onClick={() => setUser(prev => ({ ...prev, name: 'Bob' }))}>Change Name</button>
      <ChangesListener<User> listener={handleNameChange} useValue={useCurrentUser} field="name" />
    </div>
  );
}

This component provides a flexible and efficient way to respond to value changes within your React applications without cluttering your rendering logic with side-effect management.