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

@vaile/use-state-reducer

v0.2.1

Published

React useState hook with state reducer pattern support

Readme

use-state-reducer

A versatile React hook that bridges the gap between Uncontrolled State, Controlled State, and the State Reducer Pattern.
It allows components to manage their own state while giving parents the power to intercept, modify, or even cancel state updates.

📦 Installation

npm install use-state-reducer

✨ Features

  • Uncontrolled by Default: Works like a standard useState if no external value is provided.
  • Fully Controllable: Syncs perfectly with external props when a value is passed.
  • State Interception: Use externalUpdater to implement validation, logging, or custom transformation logic before the state actually changes.

🚀 Usage Examples

1. Uncontrolled Mode (Basic)

Use it just like useState. The component manages its own state.

const [state, setState] = useStateReducer(0);
// state is managed internally

2. Controlled Mode (Sync with Parent)

When a value prop is passed, the hook follows the parent's state.

function Parent() {
  const [val, setVal] = useState("Hello");
  return <Child value={val} onValueChange={setVal} />;
}

function Child({ value, onValueChange }) {
  const [state, setState] = useStateReducer("", value, (prev, next) => {
    onValueChange(next); // Sync back to parent
    return next;
  });

  return <input value={state} onChange={(e) => setState(e.target.value)} />;
}

3. Interception Mode (The "Interrupt" Pattern)

This is where this hook shines. Even in Uncontrolled mode, the parent can "intercept" the update to apply logic, like a State Reducer.

function LimitedCounter() {
  const [state, setState] = useStateReducer(
    0,
    undefined, // Keep it uncontrolled
    (prev, next) => {
      // 🛡️ Interception Logic: Max value is 10
      if (next > 10) {
        alert("Cannot exceed 10!");
        return prev; // Return previous state to "cancel" the update
      }

      console.log(`Changing from ${prev} to ${next}`);
      return next; // Apply the update
    },
  );

  return <button onClick={() => setState(state + 1)}>{state}</button>;
}

📚 API Reference

const [state, setState] = useStateReducer(initValue, value, externalUpdater?);

Parameters

| Parameter | Type | Description | | :---------------- | :------------------------------- | :---------------------------------------------------------------------------------------------------------------------- | | initValue | T | The initial state (used in Uncontrolled mode). | | value | T \| undefined | The external state. If not undefined, the hook enters Controlled mode. | | externalUpdater | (prev, next) => T \| undefined | (Optional) A function that intercepts the update. Returns the next state to apply, or undefined to skip the update. |

Returns

  • [state, setState]:
    • state: The current active state (internal if uncontrolled, external if controlled).
    • setState: A function to trigger a state update. It automatically handles the externalUpdater logic and checks for control status.

💡 Why use this over useControllableState?

Standard controllable hooks usually only switch between internal/external state. useStateReducer adds a Middleware Layer (externalUpdater).

This allows you to:

  1. Validate state changes before they happen.
  2. Transform data (e.g., forcing uppercase on an input).
  3. Trigger Side Effects (like logging or analytics) exactly when the state is requested to change, without worrying about React's render-cycle purity issues.

📄 License

MIT