Stop Rebuilding UI

Shadcn.io is not affiliated with official shadcn/ui

React useLocalStorage Hook

React useLocalStorage hook for persistent state. useState API, JSON serialization, cross-tab sync, SSR safe. TypeScript ready.

Persist React state to localStorage with useState-like API. useLocalStorage returns value, setter, and remove function—data survives page refresh. Auto-serializes with JSON, supports custom serializers for complex types. Syncs across browser tabs via storage events. Returns initial value during SSR. Handles errors gracefully—falls back to initial value if localStorage is unavailable or corrupted. Perfect for user preferences, form drafts, theme settings, or any persistent UI state. Works with Next.js App Router, SSR frameworks, and incognito mode. TypeScript generics for type-safe storage.

React useLocalStorage Hook preview

Scroll to load preview

Installation

FAQ

Was this page helpful?

Sign in to leave feedback.