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

@n0n3br/react-use-deep-compare-effect

v1.0.2

Published

React hook similar to useEffect but with deep comparison for dependencies.

Downloads

14

Readme

@n0n3br/react-use-deep-compare

A React hook that works just like useEffect, but performs a deep comparison of its dependencies, preventing unnecessary re-runs when object or array references change but their content remains the same.

💡 Why useDeepCompareEffect?

React's built-in useEffect hook performs a shallow comparison of its dependencies. This means if you pass an object or an array as a dependency, useEffect will re-run the effect every time the object/array reference changes, even if its internal content is identical.

This can lead to:

  • Unnecessary re-renders and computations: Your effect might run more often than needed, impacting performance.

  • Infinite loops: If your effect updates state that causes a new object/array reference to be created, and that object/array is a dependency, you can easily end up in a loop.

useDeepCompareEffect solves this by performing a deep comparison of your dependencies. It only re-runs your effect when the actual content of your object or array dependencies changes, not just their reference.

✨ Features

  • Deep Equality Check: Compares the actual values of objects and arrays, not just their references.

  • Circular Reference Handling: Safely handles objects with circular references to prevent infinite loops during comparison.

  • Specific Type Handling: Correctly compares Date objects by their time value and RegExp objects by their source and flags.

  • Function Reference Comparison: Like useEffect, functions are compared by reference, ensuring standard React behavior for callbacks.

  • Type-Safe: Built with TypeScript for a robust development experience.

🚀 Installation

You can install @n0n3br/use-deep-compare-effect using npm, pnpm, or yarn.

using npm npm install @n0n3br/use-deep-compare-effect

using pnpm pnpm install @n0n3br/use-deep-compare-effect

using yarn yarn add @n0n3br/use-deep-compare-effect

📖 Usage

Using useDeepCompareEffect is almost identical to using useEffect. Just import it and use it in place of useEffect.

import React, { useState } from "react";
import { useDeepCompareEffect } from "use-deep-compare-effect"; // Adjust path if not installed as a package

function MyComponent() {
  const [settings, setSettings] = useState({
    theme: "dark",
    notifications: {
      email: true,
      sms: false,
    },
    tags: ["react", "hook"],
  });

  const [effectLog, setEffectLog] = useState<string[]>([]);

  // This effect will only run when the DEEP content of 'settings' changes.
  useDeepCompareEffect(() => {
    const timestamp = new Date().toLocaleTimeString();
    setEffectLog((prev) => [
      ...prev,
      `useDeepCompareEffect ran at: ${timestamp} (Settings changed deeply)`,
    ]);
    console.log("Deeply compared effect ran:", settings);

    // Optional cleanup function
    return () => {
      console.log("Deeply compared effect cleanup");
    };
  }, [settings]); // Dependencies are objects/arrays that need deep comparison

  // --- For comparison: A standard useEffect ---
  // This effect would run every time 'settings' reference changes,
  // even if its content is the same.
  // useEffect(() => {
  //   console.log('Standard useEffect ran:', settings);
  // }, [settings]);

  const updateTheme = () => {
    setSettings((prev) => ({
      ...prev,
      theme: prev.theme === "dark" ? "light" : "dark",
    }));
  };

  const toggleEmailNotifications = () => {
    setSettings((prev) => ({
      ...prev,
      notifications: {
        ...prev.notifications,
        email: !prev.notifications.email,
      },
    }));
  };

  const addTag = () => {
    setSettings((prev) => ({
      ...prev,
      tags: [...prev.tags, `new-tag-${prev.tags.length + 1}`],
    }));
  };

  const triggerShallowUpdate = () => {
    // This creates a NEW settings object reference, but with the SAME DEEP content
    // useDeepCompareEffect WILL NOT run.
    // Standard useEffect WOULD run.
    setSettings((prev) => ({ ...prev }));
  };

  return (
    <div
      style={{
        padding: "20px",
        border: "1px solid #ccc",
        borderRadius: "8px",
      }}>
      <h2>My Component with Deep Compare Effect</h2>
      <pre>{JSON.stringify(settings, null, 2)}</pre>
      <div style={{ display: "flex", gap: "10px", marginTop: "10px" }}>
        <button onClick={updateTheme}>Update Theme (Deep Change)</button>
        <button onClick={toggleEmailNotifications}>
          Toggle Email Notif. (Deep Change)
        </button>
        <button onClick={addTag}>Add Tag (Deep Change)</button>
        <button onClick={triggerShallowUpdate}>
          Trigger Shallow Update (No Deep Change)
        </button>
      </div>

      <h3>Effect Log:</h3>
      <div
        style={{
          border: "1px solid #eee",
          padding: "10px",
          maxHeight: "150px",
          overflowY: "auto",
        }}>
        {effectLog.length === 0 ? (
          <p>No effect runs yet...</p>
        ) : (
          <ul>
            {effectLog.map((log, index) => (
              <li key={index}>{log}</li>
            ))}
          </ul>
        )}
      </div>
    </div>
  );
}

export default MyComponent;

🤝 Contributing

Contributions are welcome! If you find a bug or have an idea for an enhancement, please open an issue or submit a pull request.

📄 License

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