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

@usefy/use-controllable-state

v0.25.1

Published

A React hook for controlled/uncontrolled state — the controllable state primitive every component library needs

Readme


Overview

useControllableState is part of the @usefy ecosystem — a collection of production-ready, TypeScript-first, SSR-safe React hooks. It lets a component support both a parent-controlled value/onChange API and self-managed state from a defaultValue, with a single hook and no branching at the call site — the exact pattern used by Radix UI and Mantine.

Features

  • Controlled mode — when value is defined, the returned value mirrors it and the setter only calls onChange (the parent owns the value)
  • Uncontrolled mode — when value is undefined, the hook manages its own state seeded from defaultValue
  • useState ergonomics — the setter accepts a value or an updater function (prev) => next
  • Stable & StrictMode-safe — the setter keeps a permanent identity; onChange fires only on real changes and never double-fires under StrictMode/concurrent rendering
  • TypeScript-first — full type inference and exported types
  • Tiny & tree-shakeable — zero dependencies, published as its own package

Installation

# npm
npm install @usefy/use-controllable-state

# yarn
yarn add @usefy/use-controllable-state

# pnpm
pnpm add @usefy/use-controllable-state

Requires React 18 or 19 (peerDependencies: "react": "^18.0.0 || ^19.0.0").

Quick Start

import { useControllableState } from "@usefy/use-controllable-state";

// A switch that is controllable but works standalone.
function Switch({
  checked,
  defaultChecked,
  onCheckedChange,
}: {
  checked?: boolean;
  defaultChecked?: boolean;
  onCheckedChange?: (checked: boolean) => void;
}) {
  const [on, setOn] = useControllableState({
    value: checked,
    defaultValue: defaultChecked ?? false,
    onChange: onCheckedChange,
  });

  return (
    <button role="switch" aria-checked={on} onClick={() => setOn((p) => !p)}>
      {on ? "On" : "Off"}
    </button>
  );
}

// Uncontrolled — manages its own state:
<Switch defaultChecked />;

// Controlled — the parent owns the value:
const [value, setValue] = useState(false);
<Switch checked={value} onCheckedChange={setValue} />;

API

const [value, setValue] = useControllableState<T>({
  value,        // T | undefined — the controlled value (defined ⇒ controlled)
  defaultValue, // T | undefined — initial value used while uncontrolled
  onChange,     // ((value: T) => void) | undefined — called on every change
});

Options — UseControllableStateOptions<T>

| Option | Type | Description | | -------------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | | value | T \| undefined | The controlled value. When not undefined, the hook is controlled: the returned value mirrors this prop and the setter only notifies onChange. | | defaultValue | T \| undefined | Initial value used in uncontrolled mode (when value is undefined). Passed to useState, so a function value is treated as a lazy initializer. | | onChange | (value: T) => void | Called with the next value on every change. Its identity may change between renders without re-subscribing — the latest callback is always used. |

Returns — UseControllableStateReturn<T>

A readonly [value, setValue] tuple with the same shape as useState:

  • value: T — the effective value (the controlled prop when controlled, otherwise the internal state).
  • setValue: Dispatch<SetStateAction<T>> — accepts a next value or an updater (prev) => next. In uncontrolled mode it updates internal state and fires onChange on change; in controlled mode it only fires onChange (when the resolved value differs), leaving the parent to update value.

Behavior notes

  • Mode is per-render — it is decided each render by whether value === undefined. Switching between defined/undefined switches modes; components should avoid doing so mid-lifecycle (same caveat as React's own controlled inputs).
  • No onChange on mount — it fires only when the value actually changes (compared with Object.is).
  • StrictMode-safe — in uncontrolled mode onChange is dispatched from an effect after commit, never from inside a setState updater, so it does not double-fire.

Testing

📊 View Detailed Coverage Report (GitHub Pages) — 22 tests, 100% statement coverage.

License

MIT © mirunamu

This package is part of the usefy monorepo.