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-super-local-storage

v1.1.6

Published

A simple hook for using localStorage in React and Preact

Readme

use-super-local-storage

npm

0 dependencies · Lightweight · Type-safe · React & Preact support


use-super-local-storage is a React/Preact hook for localStorage with type-safe schema validation, encryption, cross-tab sync, and error handling—all with zero dependencies and a tiny bundle size.

  • Zero dependencies: No runtime dependencies, not even Zod or Yup.
  • Type-safe schema validation: Use a built-in, type-safe schema builder (inspired by Zod/Yup).
  • Encryption: Optional XOR encryption for keys and values.
  • Cross-tab sync: Sync state across browser tabs using BroadcastChannel.
  • Error handling: Custom error and backup strategies.
  • Works with React & Preact: For Preact, alias react/react-dom to preact/compat.

Installation

npm install use-super-local-storage
# or
yarn add use-super-local-storage

Features

  • Type-safe schema validation (no Zod/Yup required)
  • Encryption (simple XOR, optional)
  • Cross-tab synchronization
  • Error handling & backup
  • Works with React and Preact
  • Zero dependencies

Usage

1. Define your schema

import { schema, SchemaType } from "use-super-local-storage";

const userSchema = (s: SchemaType) =>
  s.object({
    name: s.string().min(2).max(32).required(),
    age: s.number().min(0).max(120).required(),
    tags: s.array(s.string()),
    profile: s.object({
      id: s.number().required(),
      admin: s.boolean(),
    }),
  });

2. Use the hook

import React from "react";
import { useLocalStorage } from "use-super-local-storage";

const initialValue = {
  name: "John Doe",
  age: 30,
  tags: [],
  profile: { id: 1, admin: false },
};

const [user, setUser] = useLocalStorage("user", userSchema, initialValue);

function App() {
  return (
    <div>
      <h1>{user.name}</h1>
      <button onClick={() => setUser({ ...user, name: "Jane Doe" })}>
        Update Name
      </button>
    </div>
  );
}

export default App;

Advanced Example: Encryption & Error Handling

const encryptConfig = {
  key: true,
  value: true,
  phrase: "my-secret-phrase",
};

const [user, setUser] = useLocalStorage("user", userSchema, initialValue, {
  encrypt: {
    key: true, // Encrypt the key in localStorage
    value: true, // Encrypt the value in localStorage
    phrase: "my-secret-phrase", // Encryption phrase (simple XOR)
  },
  onError: (error) => console.error("LocalStorage Error:", error),
  restoreOnError: true, // Restore to initial value on error (default: true)
  useBroadcastChannel: true, // Sync across tabs (default: true)
  backupOnError: true, // Store a backup with a timestamped key on error
  debug: true, // Enable debug logging
  onChangeBeforeValidation: (oldValue, newValue) => {
    // Called before validation
  },
  onChangeAfterValidation: (oldValue, newValue) => {
    // Called after validation
  },
});

API

useLocalStorage

import { SchemaType, LocalStorageConfiguration } from "use-super-local-storage";

type UseLocalStorageType = <R>(
  key: string,
  schema: (builder: SchemaType) => { parse(value: unknown): R },
  initialValue: R,
  config?: LocalStorageConfiguration<R>
) => [R, React.Dispatch<React.SetStateAction<R>>];

Parameters

  • key (string): The key used in localStorage.
  • schema (function): Receives the schema builder (SchemaType) and returns a schema instance.
  • initialValue (R): The initial value if nothing is found in localStorage.
  • config (LocalStorageConfiguration<R>): Optional configuration object.

Configuration Options (LocalStorageConfiguration<T>)

  • onError?: (value: any) => void
    Handles errors that occur during local storage operations.
  • onChangeAfterValidation?: (oldValue: T, newValue: T) => void
    Called after the value has been validated.
  • onChangeBeforeValidation?: (oldValue: T, newValue: any) => void
    Called before the value is validated and set.
  • restoreOnError?: boolean
    Restore to initial value if an error occurs. Default: true.
  • useBroadcastChannel?: boolean
    Sync local storage across tabs. Default: true.
  • encrypt?: { key?: boolean; value?: boolean; phrase: string }
    Basic encryption for key/value (simple XOR, not secure for sensitive data).
  • backupOnError?: boolean
    Store a backup with a timestamped key on error.
  • debug?: boolean
    Enable debug mode to log local storage operations.

Returns

  • [storedValue, setValue]: The stored value and a setter function.

Notes

  • Encryption: Uses a simple XOR cipher. Not secure for sensitive data.
  • Validation: Uses a built-in schema builder (not Zod/Yup).
  • Preact: Alias react and react-dom to preact/compat for full compatibility.

License

MIT