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-state-multiple

v1.0.0

Published

React state hook for multiple values

Downloads

8

Readme

use-state-multiple

Table of Contents

Introduction

use-state-multiple provides a mechanism for reading and writing multiple component state values. The library's hook replaces multiple useState() calls with one and replaces multiple data setters with also one.

Installation

Installing the Library

To fetch the library, run the following command.

npm install --save use-state-multiple

Distributed Versions

use-state-multiple's default import is either an EcmaScript (ES) or a CommonJS (as an UMD) module that bundles the source code without transpilation. The library makes use of nullish coalescing operator (??) and the latest native methods (e.g., Object.hasOwn). The defaults are provided as such with the expectation that the library will be augmented as a dependency to a host project that, in turn, will be transpiled for some target environment or used, as is, in a browser or server-side environment (e.g., Node 20+) that supports the utilized language features. For those rare circumstances when use-state-multiple has to be utilized in older browsers, the EcmaScript 5 distributable is available from use-state-multiple\es5.

Usage

Invoking the Hook

useStateMultiple() accepts an initial state (default is a plain object) and returns an array of current state and state updating and resetting functions. The current state can be destructured into individual pieces.

import {useStateMultiple} from 'use-state-multiple';

export function Component() {
  let [state, patchState, resetState] = useStateMultiple();
  let {phone = '', username = '', email = ''} = state;

  /*
    ...
  */
}

Updating Component State

Multiple Updates

The updating function can take an object of data addresses and their new values. The library supports a deeply nested state object and an address can be either a dot-delimited path (e.g., user.loggedIn), an array (e.g., ['user', 'loggedIn']), or singular (e.g., loggedIn). A function can be specified instead of a value. The function will be given a current state value stored at an address and the function's return will be then stored at the address.

export function Component() {
  let [state, patchState] = useStateMultiple();
  let {count = 0, username = '', email = ''} = state;

  /*
    ...
  */

  patchState({
    count(count = 0) {
      return count + 1;
    },
    email: '[email protected]'
  });
}

Single Update

For singular updates, the updating function also accepts a data address and its new value. Instead of a value, a transforming function can be provided that takes an existing value, operates on it, and returns a new value.

export function Component() {
  let [state, patchState] = useStateMultiple();
  let {user: {loggedIn: false, email: ''} = {}, permissions = []} = state;

  /*
    ...
  */

  patchState('user.loggedIn', true);
}

Updating Process

patchState() transforms its input into an array of address and value pairs. It then mutates a state one address at a time. A new value is written only if it is different from an existing one. Strict equality (===) is used when performing the comparison.

Resetting Component State

The resetting function may be used to overwrite an entire state at once. The function's default parameter is the initial state used with useStateMultiple(). If a different data state is needed, then it should be passed to the resetter directly.

export function Component() {
  let initialState = {loggedIn: true, username: 'username'};
  let [state, patchState, resetState] = useStateMultiple(initialState);

  /*
    ...
  */

  resetState(); // sets state to initialState
}
export function Component() {
  let initialState = {loggedIn: true, username: 'username'};
  let [state, patchState, resetState] = useStateMultiple(initialState);

  /*
    ...
  */

  resetState({loggedIn: false}); // sets an entirely new state
}

Using an Array to Store a State

An array can be used to hold a state.

export function Component() {
  let [state, patchState] = useStateMultiple([]);
  let [loggedIn = false, email = ''] = state;

  /*
    ...
  */

  patchState({0: true, 1: '[email protected]'});
}

Development

Development Setup

Perform the following steps to setup the repository locally.

git clone https://github.com/aptivator/use-state-multiple.git
cd use-state-multiple
npm install

To start development mode run npm run dev or npm run dev:coverage.

Contributing Changes

The general recommendations for contributions are to use the latest JavaScript features, have tests with complete code coverage, and include documentation. The latter may be necessary only if a new feature is added or an existing documented feature is modified.