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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-equality-hooks

v0.1.5

Published

*Drop-in React hooks with pluggable dependency equality.* Choose how deps are compared: **identity** (`Object.is`), **shallow**, **deep** (default), or **custom**, for `useMemo`, `useCallback`, `useEffect`, and friends.

Readme

react-equality-hooks

Drop-in React hooks with pluggable dependency equality. Choose how deps are compared: identity (Object.is), shallow, deep (default), or custom, for useMemo, useCallback, useEffect, and friends.

react-equality-hooks banner


Getting Started

Install with your preferred package manager:

npm install react-equality-hooks

or, if using pnpm:

pnpm add react-equality-hooks

☕ 60-Second TL;DR

Minimal example you can paste into a component:

import { useMemo, useCallback } from 'react-equality-hooks';

function heavy(user: { name: string; age: number }) {
  // pretend this is expensive
  return `${user.name} (${user.age})`;
}

export default function Demo() {
  const user = { name: 'Alice', age: 30 }; // recreated each render

  // Deep (structural) compare is the default → won't recompute unless user "value" changes
  const label = useMemo(() => heavy(user), [user]);

  // Pick a strategy explicitly
  const labelShallow = useMemo(() => heavy(user), [user], 'shallow');

  // You can always use the old behavior of standar useMemo
  const labelIdentity = useMemo(() => heavy(user), [user], 'identity');

  // Or provide a custom comparator for deps
  const handler = useCallback(
    () => console.log('submit for', user.name),
    [user],
    (prev, next) => prev[0].name === next[0].name // only react to name changes
  );

  return (
    <div>
      <div>{label}</div>
      <div>{labelShallow}</div>
      <button onClick={handler}>Submit</button>
    </div>
  );
}

Usage

A more detailed example across hooks:

import {
  useMemo,
  useCallback,
  useEffect,
  // optional parity hooks:
  useLayoutEffect,
  useInsertionEffect,
  useImperativeHandle,
} from 'react-equality-hooks';

// 1) Default: deep compare (structural)
const data = useMemo(() => build(userProfile), [userProfile]);

// 2) Shallow: top-level only for objects/arrays
const onChange = useCallback(() => save(form), [form], 'shallow');

// 3) Identity: same as React’s default (Object.is per dep)
useEffect(() => {
  const sub = api.subscribe(params);
  return () => sub.unsubscribe();
}, [params], 'identity');

// 4) Custom: compare deps arrays however you like
useLayoutEffect(() => {
  doLayout(layout);
}, [layout], (prev, next) => prev[0].version === next[0].version);

Tip: If you need both the React built-ins and these, you can alias: import { useMemo as useMemoBy } from 'react-equality-hooks'.

API Reference

Comparison strategies

  • 'identity' – per-dependency check via Object.is(a, b) (React’s semantics).
  • 'shallow' – compare only the first level of objects/arrays; nested values by identity.
  • 'deep' – structural/value compare (recursive). Default.

You can also pass a custom comparator:

type Comparator = (prevDeps: readonly unknown[], nextDeps: readonly unknown[]) => boolean;
// Return true → "equal" (skip); false → "changed" (re-run)

Hooks

useMemo<T>(factory, deps, compare?)

Compute a memoized value based on deps and the chosen equality.

Parameters:

| Parameter | Type | Description | |-----------|---------------------------------------------------|----------------------------------------------------| | factory | () => T | Function that produces the value. | | deps | readonly unknown[] | Dependency array. | | compare | 'identity' \| 'shallow' \| 'deep' \| Comparator | Strategy or custom comparator. (Default: 'deep') |

Returns: T

useCallback(fn, deps, compare?)

Returns a memoized callback; same parameters/compare semantics as useMemo.

useEffect(effect, deps, compare?)

Runs the effect when deps “change” under the chosen equality. Note: If your comparator deems deps “equal,” the effect won’t re-run and its cleanup won’t run. Choose comparators carefully for subscriptions/timers.

Parity hooks

All accept the same compare? third parameter:

  • useLayoutEffect(effect, deps, compare?)
  • useInsertionEffect(effect, deps, compare?)
  • useImperativeHandle(ref, createHandle, deps, compare?)

Ergonomic shortcuts (optional exports)

useMemoDeep / useMemoShallow / useMemoIdentity
useCallbackDeep / useCallbackShallow / useCallbackIdentity
useEffectDeep / useEffectShallow / useEffectIdentity
useLayoutEffectDeep / useLayoutEffectShallow / useLayoutEffectIdentity
useInsertionEffectDeep / useInsertionEffectShallow / useInsertionEffectIdentity
useImperativeHandleDeep / useImperativeHandleShallow / useImperativeHandleIdentity

Helpers

import { useStableValue } from 'react-equality-hooks';

// Stabilize a single value by chosen equality (useful for prop objects)
const stableOptions = useStableValue(options, 'shallow'); // or custom (prev, next) => boolean

🤝 Contributions

Contributions are welcome! Feel free to:

  1. Fork the repository
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Please follow existing coding styles and clearly state your changes in the pull request.

❓ FAQ

Why not always use deep? Deep compares are convenient but can be CPU-heavy. Prefer identity/shallow on hot paths or stabilize inputs upstream.

Does this replace React.useMemo? No, but it complements it. You can stabilize props with these hooks or export useMemo helpers (useMemoDeep/useMemoShallow) if you choose.

Issues

If you encounter any issue, please open an issue here.

License

Distributed under the MIT License. See LICENSE for details.

© 2025 Hichem Taboukouyout


If this package helped you, a star would be awesome! ⭐️