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

v1.0.1

Published

A React hook for using a unified state, with optional state diff logging

Readme

useSingleState

A React hook for using a unified state, with optional state diff logging

Using a single state object with React hooks allows you to update multiple state values in one call in functional components, similar to how setState updates state in class components.

We can also reset the state to its initial value, replace it altogether, and log state diffs to the console.

Installation

npm install use-single-state

or

yarn add use-single-state

Basic usage

import useSingleState from 'use-single-state';

const [{ text, value, note }, update] = useSingleState({
	text: '',
	value: 0,
	note: ''
});

update({
	note: 'New note'
});

Resetting state

  • You can reset the whole state back to its initial value with the reset function.
const [{ text, value }, update, reset] = useSingleState(
	{
		text: '',
		value: 0
	},
	'My component'
);

update({
	text: 'New text',
	value: 5
});

// State is now { text: 'New text', value: 5 }

reset();

// State is now { text: '', value: 0 }

Diff logging

  • useSingleState can optionally log diffs to your state to your console. The format of the diff output is modelled after redux-logger.
  • This is turned off by default. To enable this, set a label for your hook with the label parameter. You can override this with the last parameter enableLog, which defaults to true.

Example code:

const [{ text, value }, update] = useSingleState(
	{
		text: '',
		value: 0
	},
	'My component'
);

update({
	text: 'New text'
});

Replacing state

  • You can set the entire state with the replace function. Unlike update, the state is replaced and not merged.
const [{ text, value }, update, reset, replace] = useSingleState(
	{
		text: '',
		value: 0
	},
	'My component'
);

replace({
	text: 'New text'
});

// State is now { text: 'New text' }. 'value' has been discarded.

Comparison to useState and useReducer

  • useSingleState will often be less verbose than having multiple uses of useState.
  • useSingleState will only ever result in a single state update being sent to React's batcher when you need to update multiple state values at a time, likely saving your component from unnecessary re-renders.
// With useState
const [text, setText] = useState('');
const [value, setValue] = useState(0);
const [note, setNote] = useState('');

setNote('New note');
setText('Updated text');
setValue(9);

// With useSingleState
const [{ text, value, note }, update] = useSingleState({
	text: '',
	value: 0,
	note: ''
});

update({
	note: 'New note',
	text: 'Updated text',
	value: 9
});
  • useSingleState works well in situations where your component isn't complex enough to warrant managing your state with useReducer, but you still want to update multiple state values at once.
/// With useReducer
const reducer = (state, action) => {
	switch (action.type) {
		case 'fail':
			return {
				loading: false,
				error: action.error,
				data: null
			};
		case 'loading': {
			return {
				loading: true,
				error: null,
				data: null
			};
		}
		case 'success': {
			return {
				loading: false,
				error: null,
				data: action.data
			};
		}
		default:
			throw new Error();
	}
};

const [{ data, loading, error }, dispatch] = useReducer(reducer, {
	loading: false,
	data: null,
	error: null
});

dispatch({
	type: 'loading'
});

dispatch({
	type: 'success',
	data
});

// With useSingleState
const [{ loading, data, error }, update] = useSingleState({
	loading: false,
	data: null,
	error: null
});

update({
	loading: true
});

update({
	loading: false,
	data
});