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

use-state-array

v1.2.0

Published

hook to manage array as state in React application

Readme

use-state-array

use-state-array is a lightweight helper hook for React that wraps useState and gives you array-specific helpers such as sorting, deduplication, equality checks, and resetting to the initial value. Provide your own compareTo function once and you get a predictable set of utilities for any array-based state.

Features

  • Custom ordering: Pass a comparator and the hook keeps the array sorted (or not, you decide).
  • Idempotent inserts: addItem replaces existing matches before inserting, preventing duplicates.
  • Safe removals: removeItem removes one or multiple items in a single call.
  • Equality helpers: areEquals and areEqualsDeep ensure quick comparisons in memoised components.
  • State recovery: resetArray restores the original initialState snapshot at any time.
  • Deduplication: toSingleOccurrence gives a copy of the array with unique items only.

Installation

npm install use-state-array
# or
pnpm add use-state-array

This hook works with React 16.8+ (hooks support). No extra providers or setup required.

Usage

The hook is exported as a named function. Import it in any React component and provide the comparator you want to use:

import { useStateArray } from "use-state-array";

type Donut = { id: string; flavor: string };

const initialDonuts: Donut[] = [
    { id: "1", flavor: "Chocolate Hazelnut" },
    { id: "2", flavor: "Raspberry Jelly" },
];

export function DonutList() {
    const {
        array: donuts,
        addItem,
        removeItem,
        areEqualsDeep,
        resetArray,
    } = useStateArray<Donut>(
        (a, b) => a.id.localeCompare(b.id),
        initialDonuts,
    );

    const addRandomDonut = () => {
        const flavors = [
            "Maple Bacon",
            "Lemon Poppy Seed",
            "Matcha Green Tea",
            "Cinnamon Sugar",
        ];
        const randomFlavor = flavors[Math.floor(Math.random() * flavors.length)];
        addItem({
            id: crypto.randomUUID(),
            flavor: randomFlavor,
        });
    };

    return (
        <div>
            <button onClick={addRandomDonut}>Add donut</button>
            <button onClick={resetArray} disabled={areEqualsDeep(initialDonuts)}>
                Reset
            </button>
            <ul>
                {donuts.map((donut) => (
                    <li key={donut.id}>
                        {donut.flavor}
                        <button onClick={() => removeItem(donut)}>Remove</button>
                    </li>
                ))}
            </ul>
        </div>
    );
}

Want to manage the array manually? Pass keepSorted = false to preserve insertion order:

const state = useStateArray(compareFn, [], false);

API

const state = useStateArray<D>(
    compareTo: (a: D, b: D) => number,
    initialState?: D[],
    keepSorted = true,
);
  • compareTo — Mandatory comparator used for sorting and equality checks.
  • initialState — Optional starting array. Defaults to [].
  • keepSorted — When true, every mutation keeps the array sorted using compareTo.

Returned helpers

  • array — The current array state.
  • setArray(items: D[]) — Replaces the array with items (sorted if keepSorted is true).
  • addItem(item: D | D[]) — Upserts an item or list of items into the array.
  • removeItem(item: D | D[]) — Removes an item or list of items from the array.
  • areEquals(items: D[]) — Shallow equality using compareTo, ignoring order and duplicates.
  • areEqualsDeep(items: D[]) — Deep equality using lodash.isequal.
  • findInArray(item: D) — Returns the first matching element or null.
  • toSingleOccurrence() — Returns a copy containing only the first occurrence of each item.
  • resetArray() — Restores the array to the provided initialState.

Refer to the in-source TSDoc comments (src/index.ts) for precise typing information.

Release

The project ships through an automated GitHub Actions workflow:

  1. Make sure package.json has the right version (use npm version <patch|minor|major>).
  2. Push a matching git tag in the form v1.2.3 to GitHub: git push origin main --tags.
  3. The Release workflow runs npm ci, builds with microbundle, and publishes to npm.

Setup required on the repository:

  • Add an NPM_TOKEN secret with publish rights to the package.
  • Optional: trigger the workflow manually from the Actions tab (Workflow Dispatch) to re-run a release.

You can still release locally with npm run release, which builds and publishes using the same steps.