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

@nicnocquee/use-local-storage-hook

v0.0.5

Published

A custom hook that simplifies storing, retrieving, and clearing any types of data from the local storage including sync with other tabs

Readme

useLocalStorage React Hook

A type-safe, composable React hook for synchronizing state with localStorage, supporting any serializable value, with cross-tab updates and legacy data migration. Powered by superjson for robust serialization.

Features

  • Type-safe: Works with any serializable value (string, number, object, array, boolean, Date, null, undefined)
  • Cross-tab sync: Updates state across browser tabs/windows
  • Legacy migration: Migrates old values to a new, safer format
  • Clear API: Get, set, and clear values with a simple tuple

Installation

npm install @nicnocquee/use-local-storage-hook superjson

Peer dependencies:

  • react >= 18.3.1
  • superjson >= 2.2.2

Usage

import { useLocalStorage } from "@nicnocquee/use-local-storage-hook";

const [value, setValue, clearValue] = useLocalStorage<string>(
  "my-key",
  "default"
);

Example: Storing String

const [name, setName] = useLocalStorage<string>("name", "Alice");

Example: Storing Object

const [user, setUser] = useLocalStorage<{ id: number; name: string }>("user", {
  id: 1,
  name: "Alice"
});

Example: Storing Array

const [items, setItems] = useLocalStorage<number[]>("items", [1, 2, 3]);

Example: Storing Boolean

const [flag, setFlag] = useLocalStorage<boolean>("flag", false);

Example: Clearing a value

const [value, setValue, clearValue] = useLocalStorage("key", "default");
clearValue(); // value becomes undefined

By default, the value is not actually deleted from localStorage, it just sets it to "cleared" type. In this case, the "data" variable will return undefined.

If you want to actually delete the value from localStorage, you can call the clearValue function with purge set to true.

const [value, setValue, clearValue] = useLocalStorage("key", "default");
clearValue(true); // value becomes "default" because it's the initial value

API

const [value, setValue, clearValue] = useLocalStorage<T>(key: string, initialValue?: T);
  • value: The current value from localStorage (or initialValue if the key is not found in localStorage)
  • setValue(value: T): Sets the value in localStorage
  • clearValue(purge?: boolean): Clears the value. When purge is true, the value is actually deleted from localStorage and the value variable will return the initialValue parameter. Otherwise, it just sets it to "cleared" type and the value variable will return undefined.

Testing

This project uses Vitest and @testing-library/react for tests. To run tests:

npm test

License

MIT