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

storage-react-hook

v0.1.0

Published

React hooks for localStorage and sessionStorage with a useState-like API.

Readme

storage-react-hook

Small React v18+ hooks for reading and writing window.localStorage and window.sessionStorage from React components.

The package focuses on two hooks: useLocalStorage() and useSessionStorage(). Both follow the familiar React.useState() shape: you get the current stored value and a setter function, and the setter also supports updater functions. Additionally there is a useSerializer() helper which allows storing more than simple strings.

Features

  • useLocalStorage() and useSessionStorage() hooks.
  • State-like API: [value, setValue] = useLocalStorage(key).
  • Supports updater functions: setValue(previous => next).
  • Keeps components in the same tab in sync when the same storage key changes.
  • Reacts to browser storage events from other tabs/windows.
  • Removes the storage item when the next value is null or undefined.
  • Built on React's useSyncExternalStore.

Installation

npm install storage-react-hook

Usage

Raw string storage

import { useLocalStorage, useSessionStorage } from 'storage-react-hook';

function Example() {
	const [name, setName] = useLocalStorage('name');
	const [tabId, setTabId] = useSessionStorage('tab-id');

	return (
		<>
			<input
				value={name ?? ''}
				onChange={(event) => setName(event.target.value)}
			/>

			<button onClick={() => setName(null)}>
				Clear name
			</button>

			<p>Current tab id: {tabId ?? 'not set'}</p>
		</>
	);
}

Updating from the previous value

import { useLocalStorage } from 'storage-react-hook';

function Counter() {
	const [count, setCount] = useLocalStorage('count');

	return (
		<button
			onClick={() => {
				setCount((previous) => String(Number(previous ?? 0) + 1));
			}}
		>
			Count: {count ?? '0'}
		</button>
	);
}

API

useLocalStorage(key)

Reads and writes a string value from window.localStorage.

const [value, setValue] = useLocalStorage(key);

Returns:

readonly [
	string | null,
	(
		value:
			| string
			| null
			| undefined
			| ((previous: string | null) => string | null | undefined)
	) => void,
]

useSessionStorage(key)

Reads and writes a string value from window.sessionStorage.

const [value, setValue] = useSessionStorage(key);

Returns the same tuple shape as useLocalStorage().

Removing Values

Passing null or undefined to a setter removes the storage item.

const [value, setValue] = useLocalStorage('draft');

setValue(null);
setValue(undefined);

Notes

  • This package is intended for browser environments.
  • useLocalStorage() and useSessionStorage() store raw strings.
  • Storage read/write errors are handled safely, so restricted storage access should not crash your component.

Working With JSON Values

The main hooks store strings, just like the Web Storage API. If you want to work with objects, arrays, numbers, booleans, or other JSON-serializable values, the package also includes useSerializer().

import { useLocalStorage, useSerializer } from 'storage-react-hook';

type Settings = {
	theme: 'light' | 'dark';
	showHints: boolean;
};

function SettingsPanel() {
	const [storedSettings, setStoredSettings] = useLocalStorage('settings');
	const [settings, setSettings] = useSerializer<Settings>(
		storedSettings,
		setStoredSettings,
	);

	return (
		<button
			onClick={() => {
				setSettings((previous) => ({
					showHints: previous?.showHints ?? true,
					theme: previous?.theme === 'dark' ? 'light' : 'dark',
				}));
			}}
		>
			Current theme: {settings?.theme ?? 'light'}
		</button>
	);
}

useSerializer() parses the stored string with JSON.parse() and converts updates back with JSON.stringify().

const [jsonValue, setJsonValue] = useSerializer<T>(value, setValue, options);

Since useLocalStorage() and useSessionStorage() return the expected [value, setValue] pair, you can also pass them directly with spread syntax:

const [value, setValue] = useSerializer(...useLocalStorage('storage-key'));

If the stored value is missing, jsonValue is undefined. Parse and stringify errors are not swallowed, so custom serializers should handle invalid input if that is expected.

Passing null or undefined through the serializer setter also removes the storage item:

const [storedValue, setStoredValue] = useLocalStorage('settings');
const [settings, setSettings] = useSerializer<Settings>(storedValue, setStoredValue);

setSettings(null);
setSettings(undefined);

By default, useSerializer() uses JSON.parse() and JSON.stringify(), but both functions can be replaced with an options object:

const [value, setValue] = useSerializer(
	storedValue,
	setStoredValue,
	{
		parse: value => Number(value),
		stringify: value => String(value),
	},
);

License

MIT