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

use-flags-state

v1.1.1

Published

A React hook for managing multiple boolean flags in a single state object, with individual and batch updates, and optional reset to initial values.

Downloads

17

Readme

useFlags

A React hook for managing multiple boolean flags

npm | GitHub

Installation

npm install use-flags-state

Examples and Live Demo

You can see the examples and Live Demo here.

Usage

Manage multiple boolean flags in a single state object, with individual and batch updates, and optional reset to initial values.

import { useFlagsState } from 'use-flags-state';

const { flags, setFlags, setFlag } = useFlagsState({
  isLoading: false,
  isError: false,
  isSuccess: false,
});

Default behavior (reset=true)

By default, when you update flags with setFlags(), all flags not specified in the update are reset to their initial values. This is useful when you want to ensure a clean state for each operation.

const { flags, setFlags } = useFlagsState({
  isLoading: false,
  isError: false,
  isSuccess: false,
});

const handleSubmit = async () => {
  setFlags({ isLoading: true }); // isError and isSuccess reset to false
  try {
    await submitData();
    setFlags({ isSuccess: true }); // isLoading and isError reset to false
  } catch (error) {
    setFlags({ isError: true }); // isLoading and isSuccess reset to false
  }
};

Preserve state behavior (reset=false)

When you set reset=false, only the flags you specify are updated, while all other flags maintain their current values. This is perfect for complex UIs where you need to preserve existing state.

const { flags, setFlags } = useFlagsState({
  isDarkMode: false,
  isModalOpen: false,
  isLoading: false,
}, false); // reset=false

const toggleDarkMode = () => {
  setFlags({ isDarkMode: !flags.isDarkMode }); // Other flags remain unchanged
};

const openModal = () => {
  setFlags({ isModalOpen: true }); // isDarkMode and isLoading remain unchanged
};

Individual flag updates with setFlag

The setFlag function allows you to update individual flags without affecting others. This is particularly useful when passing toggle functions as props to child components, such as modals, dropdowns, or any component that needs to control its own visibility state.

const { flags, setFlag } = useFlagsState({
  isModalOpen: false,
  isDropdownOpen: false,
  isLoading: false,
});

// Direct value
const openModal = () => setFlag('isModalOpen', true);

// Updater function
const toggleDropdown = () => setFlag('isDropdownOpen', prev => !prev);

// Pass to child components
<Modal isOpen={flags.isModalOpen} onClose={() => setFlag('isModalOpen', false)} />
<Dropdown isOpen={flags.isDropdownOpen} onToggle={setFlag('isDropdownOpen')} />

setFlag returns a function that behaves like React's setState, accepting either a boolean value or an updater function.

Key benefits of setFlag:

  • Individual updates: Only the specified flag changes, others remain untouched
  • Component props: Perfect for passing toggle functions to child components
  • Flexible API: Accepts both direct values and updater functions
  • Type safety: Full TypeScript support with autocomplete for flag names

API

useFlagsState(initialState, reset?)

Parameters:

  • initialState: Object with the initial boolean flags
  • reset (optional): Boolean that determines if unspecified flags should reset to their initial values when using setFlags(). Default: true

Returns:

  • flags: Object with the current boolean flags
  • setFlags: Function to update multiple flags at once
  • setFlag: Function to update an individual flag