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

@guoyunhe/react-storage

v2.0.3

Published

React hooks for localStorage and sessionStorage with JSON serialization, custom serializer/parser, key prefix, and SSR-safe fallback

Readme

@guoyunhe/react-storage

npm version npm downloads bundle size socket security license

React hooks for localStorage and sessionStorage — JSON serialization, custom serializer/parser, key prefix namespacing, and real-time cross-tab sync. SSR-safe with zero direct dependencies.

Features

  • 🪝 useState-like API — drop-in replacement with the same [value, setValue] tuple
  • 🔄 Cross-tab sync — state stays in sync across browser tabs via the native storage event
  • 🧩 Custom serializer/parser — store Date, Map, or any custom type
  • 📦 JSON by default — objects and arrays work out of the box
  • 🏷️ Key prefix — namespace storage keys per app via <StorageProvider>
  • 🌐 SSR-safe — no window access during render, works with Next.js and Remix
  • 🪶 Tiny bundle — zero runtime dependencies, tree-shakable ESM
  • 💪 TypeScript first — fully typed with generics

Install

npm install --save @guoyunhe/react-storage

Quick Start

import { useLocalStorage, useSessionStorage } from '@guoyunhe/react-storage';

function App() {
  const [settings, setSettings] = useLocalStorage('settings', {
    theme: 'dark',
    fontSize: 14,
  });

  const [draft, setDraft] = useSessionStorage('draft', '');
}

Guides

Custom types (Date, Map, etc.)

Use serializer and parser to handle non-JSON types:

const [date, setDate] = useLocalStorage('date', new Date(), {
  serializer: (d) => d.toISOString(),
  parser: (s) => new Date(s),
});

Key prefix for multi-app domains

Scope storage keys per app to avoid collisions:

import { StorageProvider, useLocalStorage } from '@guoyunhe/react-storage';

function App() {
  return (
    <StorageProvider prefix='dashboard_'>
      <Dashboard />
    </StorageProvider>
  );
}

function Dashboard() {
  // actual key: "dashboard_theme"
  const [theme, setTheme] = useLocalStorage('theme', 'light');
}

Global serializer/parser

Set defaults once at the top level:

<StorageProvider serializer={mySerializer} parser={myParser} prefix='app_'>
  <App />
</StorageProvider>

Cross-tab counter

All components reading the same key stay in sync — even across tabs:

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

  return (
    <div>
      <button onClick={() => setCount((c) => c - 1)}>−</button>
      <span>{count}</span>
      <button onClick={() => setCount((c) => c + 1)}>+</button>
    </div>
  );
}

// Render multiple <Counter /> — they all share the same state

API

useLocalStorage<T>(key, defaultValue, options?)

Returns [value, setValue] — same shape as useState. Values persist in localStorage and sync across tabs.

useSessionStorage<T>(key, defaultValue, options?)

Same API as useLocalStorage, but stores in sessionStorage. Data is cleared when the tab closes. Useful for draft forms, wizard state, and ephemeral UI state.

options

| Option | Type | Default | Description | | ------------ | ---------------------- | ---------------- | --------------------------------------- | | serializer | (value: T) => string | JSON.stringify | Custom serialize function | | parser | (raw: string) => T | JSON.parse | Custom parse function | | prefix | string | '' | Override the global prefix for this key |

<StorageProvider>

Provides global defaults for all hooks within its subtree.

| Prop | Type | Default | Description | | ------------ | ------------------------ | ---------------- | ----------------- | | serializer | (value: any) => string | JSON.stringify | Global serializer | | parser | (raw: string) => any | JSON.parse | Global parser | | prefix | string | '' | Global key prefix |

Comparison

| | @guoyunhe/react-storage | use-local-storage-state | usehooks-ts | react-use | ahooks | | ----------------- | :---------------------: | :---------------------: | :---------: | :-------: | :------: | | React peer | >=16.8 | >=18 | >=16.8 | * | >=16.8 | | Dependencies | 0 | 0 | 1 | 14 | 10 | | Local + Session | ✅ | ❌ | ✅ | ✅ | ✅ | | Cross-tab sync | ✅ | ✅ | ❌ | ❌ | ✅ | | Key prefix | ✅ | ❌ | ❌ | ❌ | ❌ | | Custom serializer | ✅ | ✅ | ✅ | ❌ | ✅ | | Typed API | ✅ | ✅ | ✅ | ✅ | ✅ | | ESM | ✅ | ✅ | ✅ | ✅ | ✅ |

License

MIT