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 🙏

© 2025 – Pkg Stats / Ryan Hefner

better-react-use-state

v1.0.3

Published

A utility that improves React's `useState` to be safer and more convenient. Provides both `useState` (enhanced) and a dedicated `useBoolState` hook.

Readme

better-react-use-state

A utility that improves React's useState to be safer and more convenient. Provides both useState (enhanced) and a dedicated useBoolState hook.

Overview

React's useState can cause unintended behavior when you want to hold a function as a state. better-react-use-state solves this problem and is a utility designed for safer state management. It is designed as a drop-in replacement for React's useState hook, with added functionality. better-react-use-state also provides a simplified hook, useBoolState, specifically for boolean state.

Installation

You can install it with any of the following:

npm install better-react-use-state
yarn add better-react-use-state
pnpm add better-react-use-state

Usage

useState

better-react-use-state's useState can be used the same way as React's useState.

import { useState } from 'better-react-use-state';

const MyComponent: React.FC = () => {
    const [name, setName] = useState('John Doe');

    const [count, setCount, { updateState: updateCount }] = useState(0);

    const onNameChange = React.useCallback((event) => {
        setName(event.target.value);
    }, []);

    const incrementCount = React.useCallback(() => {
        updateCount((x) => x + 1);
    }, []);

    return (
        <div>
            <p>{`Count: ${count}`}</p>
            <button onClick={incrementCount}>{'Increment'}</button>

            <p>{`Name: ${name}`}</p>
            <input type='text' value={name} onChange={onNameChange} />
        </div>
    );
};

useBoolState

useBoolState simplifies managing boolean state.

import { useBoolState } from 'better-react-use-state';

const MyComponent: React.FC = () => {
    const [isOpen, { setTrue: open, setFalse: close, toggleState }] =
        useBoolState(false);

    return (
        <div>
            <p>{`Is Open: ${isOpen ? 'Yes' : 'No'}`}</p>
            <button onClick={open}>{'Open'}</button>
            <button onClick={close}>{'Close'}</button>
            <button onClick={toggleState}>{'Toggle'}</button>
            {/* Toggles the boolean value */}
        </div>
    );
};

API Reference

useState

const [state, setState, { updateState, resetState }] = useState<T>(initialState: T);
  • state: The current state.
  • setState: Function to update the state.
  • updateState: Function to update the state based on the current state.
  • resetState: Function to reset the state to the initial value.

updateState

updateState(updateFn: (v: T) => T): void;

Updates the state by passing a function updateFn that takes the current state as an argument and returns the new state.

resetState

resetState(): void;

Resets the state to the initial value.

useBoolState

const [state, { setState, setTrue, setFalse, resetState, toggleState, updateState }] = useBoolState(initialState: boolean);
  • state: The current boolean value.
  • setState: Function to update the boolean value directly.
  • setTrue: Function to set the state to true.
  • setFalse: Function to set the state to false.
  • resetState: Function to reset the state to the initial value.
  • toggleState: Function to toggle the boolean value (true to false, false to true).
  • updateState: Function to update the state based on the current state.

setState

setState(next: boolean): void;

Sets the state to the provided boolean value.

setTrue

setTrue(): void;

Sets the state to true.

setFalse

setFalse(): void;

Sets the state to false.

resetState

resetState(): void;

Resets the state to the initial value.

toggleState

toggleState(): void;

Toggles the current boolean value.

updateState

updateState(updateFn: (v: boolean) => boolean): void;

Updates the state by passing a function updateFn that takes the current state as an argument and returns the new state. This is useful for updates that depend on the current state.

Benefits of this library

With React's standard useState, attempting to store a function as state can lead to unexpected behavior (the function being interpreted as an updater function). better-react-use-state solves this issue and, by providing updateState, allows functions to be safely held as state. Also, resetState lets you easily return the state to its initial value. useBoolState simplifies boolean state management in React, offering several advantages over directly using useState for booleans.

Example Use Case

Useful when you want to hold a function as state.

const [fn, setFn, { updateState: updateFn }] = useState(initialFn); // Holds a function as state

// In React's standard useState, `setFn(nextFn)` interprets nextFn as an update function.

// If you want to update the state with an update function, you can use updateState instead.
updateFn((currentFn) => nextFn);

License

MIT