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

@sincpro/criteria-react

v2.6.0

Published

React hooks over @sincpro/criteria's Reading and Buckets, plus a query-key helper for wiring a resource into an existing cache (react-query, …). See docs/DESIGN.md at the repo root for why this is a separate package.

Readme

@sincpro/criteria-react

React hooks over @sincpro/criteria's Reading/Buckets, and a query-key helper for a consumer that already has its own cache. A separate package from the core on purpose — see docs/DESIGN.md at the repo root for why — because it needs react as a real dependency, and that must never leak into installing the core for Criteria/Source alone.

There are two ways to use this, and picking between them is one question: does your app already have a query cache (react-query, SWR, …)?

No cache of your own: useReading / useBuckets

Reading and Buckets are already useSyncExternalStore-shaped stores. These hooks own the lifecycle a store by itself cannot: create one per (resource, criteria) pair, load it once, clean it up on unmount.

import { useReading } from "@sincpro/criteria-react";
import { criteria } from "@sincpro/criteria";

function DatasetList({ resource, selection }: Props) {
  const { rows, status, hasMore, more } = useReading(
    resource,
    criteria<Dataset>().selection(selection).limit(50),
  );

  if (status === "loading") return <Spinner />;
  return <List rows={rows} onEndReached={more} hasMore={hasMore} />;
}

useBuckets is the grouping counterpart:

import { useBuckets } from "@sincpro/criteria-react";

function EncodingBreakdown({ resource }: Props) {
  const { buckets, status } = useBuckets(resource, criteria<Dataset>().groupBy("encoding"));
  return <BarChart data={buckets} />;
}

Both are keyed by pack(criteria), not by object identity — a criteria literal built fresh on every render does not restart the reading.

Already have a cache: queryKeyFor

Reusing Reading here would be a second cache fighting the one you already trust. queryKeyFor is the one line that was missing — a stable key from a criteria, so two requests that ask the same question hit the same cache entry:

import { useQuery } from "@tanstack/react-query";
import { queryKeyFor } from "@sincpro/criteria-react";

function useDatasets(criteria: Criteria<Dataset>) {
  return useQuery({
    queryKey: queryKeyFor(["catalog", "datasets"], criteria),
    queryFn: ({ signal }) => datasets.page(criteria, signal),
  });
}

This package never imports react-query, SWR, or anything like them — queryKeyFor returns a plain array, so it works with whichever cache you already have.

Combining a permission with what the user typed

Merging is @sincpro/criteria's job (merge, plain functions, no React in it) — this package adds nothing to that law, it just gives you a place to call it once instead of at every call site. A small custom hook is the natural shape, not a HOC — nothing here needs to wrap a component to do this:

import { criteria, merge, plain, selection } from "@sincpro/criteria";
import { useReading } from "@sincpro/criteria-react";

function usePermissionedAccounts(resource: Resource<Account>, typed: Expression<Account>) {
  const permission = criteria<Account>().selection(
    selection.any(["region", "=", "north"], ["owner", "=", useCurrentUser()]),
  );
  const asked = merge(plain(permission), plain(criteria<Account>().selection(typed)));
  return useReading(resource, asked);
}

asked is what actually reaches the network — the user's filter narrows the permission, it can never widen past it, because selection always merges by AND. See the core README's "The law for merging two of them" for what merge does with the rest of a criteria (order, pagination, grouping), not just selection. .selection() is the method that writes that field.

Server rendering and Server Components

Both hooks render on the server. Hand them what the server already read for the same criteria — useReading(resource, criteria, { page }) with resource.page(criteria), useBuckets(resource, criteria, { page }) with resource.groups(criteria) — and the HTML carries it and hydrating asks nothing again; without it they render idle, and the browser loads them as a SPA does.

A Server Component (Next.js App Router, …) may import from @sincpro/criteria-react. The modules that call a hook start with "use client" — the first statement of each file in dist/ too, and tests/client-boundary.test.ts keeps it that way — so the hooks reach the server as client references, and queryKeyFor, which calls none, stays callable there. The hooks themselves run in a client component of your own, as any hook does:

// app/datasets/page.tsx — a Server Component
export default async function RecentDatasets() {
  const page = await datasets.page(recent);
  return <DatasetList page={page} />; // a "use client" component: useReading(…, { page })
}

Development

make init           # from the repo root — installs the whole workspace
make test           # node --test over these hooks, react-test-renderer, no DOM needed
make typecheck
make build