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-dynamic-search

v0.2.1

Published

A debounced, filterable, cache-aware search hook for React.

Readme

react-dynamic-search

A debounced, filterable, cache-aware search hook for React — built on top of TanStack Query.

Handles debouncing, request caching, filter state, and loading states so you don't have to wire them up by hand every time you build a search box.

Install

npm install react-dynamic-search @tanstack/react-query

react and @tanstack/react-query are peer dependencies — this package doesn't bundle them, so your app's existing versions are used.

You'll also need a QueryClientProvider set up somewhere near the root of your app (standard React Query setup):

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

const queryClient = new QueryClient();

function Root() {
  return (
    <QueryClientProvider client={queryClient}>
      <App />
    </QueryClientProvider>
  );
}

Usage

import { useDynamicSearch } from 'react-dynamic-search';

function ProductSearch() {
  const { query, setQuery, filters, setFilters, results, isLoading, isFetching, error } =
    useDynamicSearch({
      onSearch: (q, filters) =>
        fetch(`/api/products?q=${q}&category=${filters.category ?? ''}`).then((r) => r.json()),
      debounceTime: 400,
      minQueryLength: 2,
      initialFilters: { category: '' },
      queryKey: ['productSearch'],
    });

  return (
    <div>
      <input value={query} onChange={(e) => setQuery(e.target.value)} />
      {isLoading && <p>Loading...</p>}
      <ul>{results.map((r) => <li key={r.id}>{r.name}</li>)}</ul>
    </div>
  );
}

Works the same in plain JavaScript/JSX or TypeScript/TSX — see examples/ for both.

API

useDynamicSearch(config)

| Option | Type | Default | Description | |---|---|---|---| | onSearch | (query, filters) => Promise<T[]> | required | Called with the debounced query and current filters. | | debounceTime | number | 500 | Milliseconds to wait after typing stops before searching. | | minQueryLength | number | 2 | Minimum query length before a search fires. | | initialFilters | object | {} | Starting filter values. | | queryKey | QueryKey | ['dynamicSearch'] | React Query cache key prefix. Use a unique key such as ['productSearch'] or ['userSearch'] when multiple search hooks can coexist. |

Returns

| Field | Type | Description | |---|---|---| | query | string | Current raw input value. | | setQuery | (value: string) => void | Update the query. | | filters | object | Current filter values. | | setFilters | React.Dispatch<SetStateAction> | Update filters. | | results | T[] | Latest search results (empty array if none yet). | | isLoading | boolean | True only on the very first load. | | isFetching | boolean | True during any fetch, including background refetches — use this for a subtle "updating" indicator without a full loading state on every keystroke. | | error | unknown | Error from the last failed search, if any. |

Design notes

  • Debounce is hand-rolled internally — a small, well-understood mechanism, not worth pulling in a separate dependency for.
  • Caching, request dedup, and stale-request handling are delegated to React Query rather than reimplemented — its queryKey includes a configurable prefix plus the debounced query and filters, so separate search components can avoid cache collisions while filter changes still bust the cache instead of serving stale results.
  • No pagination by default — kept out until there's an actual need (YAGNI); add it at the consumer level via onSearch if your API paginates.

Development

npm install
npm run dev        # watch mode build
npm run typecheck
npm run test
npm run build       # production build → dist/

License

MIT