Join our Discord Community

React useReadLocalStorage Hook

React useReadLocalStorage hook for localStorage reading. Get reactive values with cross-tab synchronization and SSR support using TypeScript for Next.js.

Need help reading localStorage safely?

Join our Discord community for help from other developers.


Ever tried to read localStorage in React and ended up with stale data, broken SSR hydration, or missing cross-tab updates? You know the drill—manually calling localStorage.getItem(), forgetting about JSON.parse errors, dealing with server-side rendering mismatches, missing storage events from other tabs. This free open source React useReadLocalStorage custom hook handles all that localStorage reading complexity so you can focus on building reactive features instead of debugging storage synchronization edge cases in your React applications.

useReadLocalStorage showcase

Reactive localStorage reading with cross-tab synchronization:

Loading component...

This free open source React hook simplifies localStorage access with TypeScript support for modern JavaScript applications. Whether you're building user preferences, authentication state, or shopping cart displays in your Next.js projects, this React hook keeps your localStorage data reactive and reliable.

Installation

npx shadcn@latest add https://www.shadcn.io/registry/use-read-local-storage.json
npx shadcn@latest add https://www.shadcn.io/registry/use-read-local-storage.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/use-read-local-storage.json
bunx shadcn@latest add https://www.shadcn.io/registry/use-read-local-storage.json

Why most localStorage reading implementations suck

Look, you could keep calling localStorage.getItem() and JSON.parse() manually. But then you hit the localStorage complexity—stale data when other tabs update values, broken SSR hydration, performance issues from repeated storage access, error handling for malformed JSON in React applications.

Most developers call localStorage.getItem() on every render, causing unnecessary performance overhead in TypeScript components. Or they forget about storage events and miss updates when other tabs change values, leading to stale data and inconsistent UI states. Some skip SSR considerations entirely and get hydration mismatches when localStorage values differ from server-rendered content in Next.js projects.

This React hook uses optimized storage event listening under the hood, with automatic cross-tab synchronization and proper SSR handling in JavaScript applications. The browser handles all the storage events, plus you get type-safe deserialization and error recovery in one call.

Plus it handles all the edge cases—SSR compatibility with controlled hydration, automatic JSON parsing with fallbacks, storage event coordination across browser tabs in React development. No more stale localStorage reads or broken server-side rendering.

This free open source React hook manages localStorage state while you focus on building features. Whether you're creating React applications, Next.js dashboards, or TypeScript components, reliable storage reading keeps your JavaScript development smooth.

Features

  • Read-only access providing lightweight alternative to full localStorage hooks in React applications
  • Automatic updates responding to localStorage changes across tabs and windows in TypeScript components
  • SSR compatible with optional initialization for server-side rendering safety in Next.js projects
  • Custom deserialization supporting custom deserializer functions for complex types in JavaScript development
  • Type safety with full TypeScript support and proper generic constraints for React frameworks
  • Error handling providing graceful fallbacks for localStorage access errors in modern applications
  • Free open source designed for modern React development workflows

When you'll actually use this

Real talk—this isn't for every localStorage need in React applications. If you're also writing to localStorage, use the full useLocalStorage hook. But when you need read-only access with reactive updates and optimal performance, this React hook delivers in Next.js projects.

Perfect for:

  • User preference displays - Show current theme, language, or layout settings built with TypeScript
  • Authentication status - Display login state and user information from tokens using React patterns
  • Shopping cart indicators - Show cart item counts and totals in navigation in JavaScript applications
  • Feature flag reading - Check enabled features without modification capabilities in React components
  • Recent activity - Display browsing history or recently viewed items in Next.js applications
  • Form draft recovery - Show saved draft indicators without editing the drafts using TypeScript safety

API Reference

useReadLocalStorage

useReadLocalStorage<T>(
  key: string,
  options?: UseReadLocalStorageOptions<T>
): T | null | undefined
ParameterTypeDefaultDescription
keystringrequiredThe localStorage key to read from
optionsUseReadLocalStorageOptions<T>{}Configuration options for the hook

Options

PropertyTypeDefaultDescription
deserializer(value: string) => TJSON.parseCustom function to deserialize the stored string value
initializeWithValuebooleantrueWhether to initialize with localStorage value (set to false for SSR)

Return Value

Returns the stored value of type T, null if key doesn't exist or error occurs, or undefined during SSR when initializeWithValue: false.

Common gotchas

Cross-tab updates are automatic: The React hook listens for storage events and updates when localStorage changes in other browser tabs in React applications. This keeps your UI synchronized but means components will re-render when external changes occur.

SSR requires careful initialization: Use initializeWithValue: false to prevent hydration mismatches in TypeScript components. The React hook will return undefined initially and update with the stored value after client hydration.

Error handling returns null: If localStorage access fails (disabled storage, quota exceeded, malformed JSON), the hook returns null in Next.js projects. Always check for null values in your components.

Read-only hook for performance: This React hook is optimized for reading only in JavaScript applications. If you need to write localStorage values, use the full useLocalStorage hook instead of combining this with manual localStorage.setItem() calls.

Storage event timing: Storage events are asynchronous and may not fire immediately when localStorage changes in React frameworks. Handle loading states appropriately for better user experience.

Deserialization error handling: Custom deserializers should handle malformed data gracefully in TypeScript projects. The hook returns null when deserialization fails, preventing application crashes.

Questions you might have