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

@opencookies/react

v0.0.2

Published

React adapter for OpenCookies. Wraps [`@opencookies/core`](../core/) with `useSyncExternalStore` for concurrent-safe reactivity.

Downloads

227

Readme

@opencookies/react

React adapter for OpenCookies. Wraps @opencookies/core with useSyncExternalStore for concurrent-safe reactivity.

Install

bun add @opencookies/core @opencookies/react

Peer dependencies: react >= 18.

Setup

Wrap your app with <OpenCookiesProvider>:

import { OpenCookiesProvider } from "@opencookies/react";
import type { Category } from "@opencookies/core";
import { createRoot } from "react-dom/client";

const categories: Category[] = [
  { key: "essential", label: "Essential", locked: true },
  { key: "analytics", label: "Analytics" },
  { key: "marketing", label: "Marketing" },
];

createRoot(document.getElementById("root")!).render(
  <OpenCookiesProvider config={{ categories }}>
    <App />
  </OpenCookiesProvider>,
);

Pass a pre-created store with <OpenCookiesProvider store={store}> instead — useful for SSR-time hydration of decisions from cookies.

API

useConsent()

Returns the current consent state plus action methods. Re-renders the consumer when state changes.

import { useConsent } from "@opencookies/react";

function Banner() {
  const { route, acceptAll, acceptNecessary, setRoute } = useConsent();
  if (route !== "cookie") return null;

  return (
    <div className="banner">
      <button onClick={acceptNecessary}>Necessary only</button>
      <button onClick={acceptAll}>Accept all</button>
      <button onClick={() => setRoute("preferences")}>Customize</button>
    </div>
  );
}

useCategory(key)

Granular per-category access. Returns { granted, toggle }.

import { useCategory } from "@opencookies/react";

function AnalyticsToggle() {
  const { granted, toggle } = useCategory("analytics");
  return (
    <label>
      <input type="checkbox" checked={granted} onChange={toggle} />
      Analytics
    </label>
  );
}

<ConsentGate>

Renders children when the expression is satisfied; renders fallback otherwise. The component itself emits no DOM wrapper.

import { ConsentGate } from "@opencookies/react";

<ConsentGate requires="analytics" fallback={<EnablePrompt />}>
  <Chart />
</ConsentGate>;

<ConsentGate requires={{ and: ["analytics", "marketing"] }}>
  <PersonalizedPromo />
</ConsentGate>;

The requires shape is a ConsentExpr from core: a category key, { and: [...] }, { or: [...] }, or { not: ... }.

Next.js

Mark the provider as a client component and mount it in your root layout:

// app/providers.tsx
"use client";
import { OpenCookiesProvider } from "@opencookies/react";
import type { Category } from "@opencookies/core";

const categories: Category[] = [
  { key: "essential", label: "Essential", locked: true },
  { key: "analytics", label: "Analytics" },
];

export function Providers({ children }: { children: React.ReactNode }) {
  return <OpenCookiesProvider config={{ categories }}>{children}</OpenCookiesProvider>;
}
// app/layout.tsx
import { Providers } from "./providers";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html>
      <body>
        <Providers>{children}</Providers>
      </body>
    </html>
  );
}

For SSR-resolved decisions, build the store on the server with the cookie/header storage adapter and pass it via store={store} instead of config={config}.

Shared concepts

Categories, GPC handling, jurisdiction resolvers, re-consent triggers, script gating (gateScript), and storage adapters all live in @opencookies/core — the React adapter is a thin reactivity wrapper. A working example is in examples/react.

See also

License

Apache-2.0