Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@piplup/use-local-storage

sadik-malik97MIT3.2.4TypeScript support: included

React hook for reading values from the browser localStorage and listening to changes in it.

@piplup/use-local-storage, useLocalStorage, reactive localStorage, react localStorage

readme

@piplup/use-local-storage

npm bundle size

⚠️ Deprecation Warning

Important Notice: useLocalStorage hook has been moved to @piplup/utils, While it may continue to work for existing projects, we recommend migrating to the new package.

useLocalStorage - Use your browser localStorage as a reactive storage. It's a hook for reading values from the browser localStorage and listening to changes in it. Any updates made to localStorage using the setItem and removeItem helper functions will trigger a state update for the useLocalStorage hook.

Installation

npm install @piplup/use-local-storage

# or

yarn add @piplup/use-local-storage

# or

pnpm add @piplup/use-local-storage

Usage

import * as React from 'react';
import { useLocalStorage } from '@piplup/use-local-storage';

function App() {
  // Usage
  const value = useLocalStorage('myKey', 'defaultValue');

  return (
    <div>
      <p>Stored Value: {value}</p>
    </div>
  );
}

API

useLocalStorage(key: string, initialValue: null | string = null): null | string

A hook for reading from and listening to changes in localStorage.

  • key: The key under which to store the value in localStorage.
  • initialValue: The initial value to be used if no value is stored in localStorage under the specified key.

Returns the stored value or null if no value is stored.

getItem(key: string): null | string

A wrapper around localStorage.getItem. On the server, localStorage is not available so a null value is returned. On the client side, the actual value will be read and returned.

setItem(key: string, value: string): void

A wrapper around localStorage.setItem. On the server, localStorage is not available so no operation will be performed. On the client side, the value will be set in localStorage, and an event will be dispatched which will be helpful for triggering side effects in the useLocalStorage hook.

removeItem(key: string): void

A wrapper around localStorage.removeItem. On the server, localStorage is not available so no operation will be performed. On the client side, the value will be removed from localStorage, and an event will be dispatched which will be helpful for triggering side effects in the useLocalStorage hook.

License

This project is licensed under the MIT License - see the LICENSE file for details.

changelog

3.2.4 (2025-05-27)

This was a version bump only, there were no code changes.

3.2.3 (2025-05-18)

🚀 Features

  • updated react version for react 19 support (c7a9353)

🩹 Fixes

  • type definitions export (ec9f5a5)
  • typo in @piplup/rhf-core/utils path (a02e614)
  • imports from nested path for @piplup/rhf-adapters and @piplup/rhf-core (ecfdb53)
  • rhf-adapters: exports fields path for package import (1362c33)
  • rhf-core: do not override error value if provided (f94eb1a)

❤️ Thank You

  • sadik-malik

3.2.2 (2024-12-16)

🩹 Fixes

  • rhf-adapters: exports fields path for package import (1362c33)

❤️ Thank You

  • sadik-malik

3.2.1 (2024-12-16)

This was a version bump only, there were no code changes.

3.2.0 (2024-12-16)

🚀 Features

  • updated react version for react 19 support (c7a9353)

❤️ Thank You

  • sadik-malik

3.1.2 (2024-11-15)

🩹 Fixes

  • imports from nested path for @piplup/rhf-adapters and @piplup/rhf-core (ecfdb53)

❤️ Thank You

  • sadik-malik

3.1.1 (2024-11-14)

🩹 Fixes

  • type definitions export (ec9f5a5)
  • typo in @piplup/rhf-core/utils path (a02e614)

❤️ Thank You

  • sadik-malik

3.1.0 (2024-11-12)

  • Bump package version for "mui-chips-input", "mui-color-input" and "mui-tel-input"

3.0.0 (2024-11-12)

  • Minimum version for packages from "@mui" is now v6.0.0

2.8.1 (2024-11-04)

🩹 Fixes

  • rhf-adapters: update mui-chips-input as optional peerDependency (1ba881e)

❤️ Thank You

  • sadik-malik

2.8.0 (2024-11-03)

🚀 Features

  • bump package version (8c74c30)
  • added playground to .gitignore and .nxignore (80487af)
  • update package.json (a86e22e)
  • make children prop optional (3c89cda)
  • update README.md (5230a7d)
  • bump package version (acbb25e)
  • added shebang line to script (78aec77)
  • move generate-release-id script to bin (ad83edb)
  • bump package version (8ae853b)
  • move package to stable version (0e21e93)
  • bump package version (71e99bd)
  • bump package version (ec3148d)
  • added use-event-listener package (8a44257)
  • added use-local-storage (85847ac)
  • change default import to name import (14b96bf)
  • added useLocalStorage hook (153ed88)
  • added .eslintignore to playground (3a0552e)
  • added package @piplup/react-acl (8653496)
  • update README.md for @piplup/react-acl (1264ccb)
  • added code of conduct markdown (d69c41a)
  • updated README.md (7ffc21c)
  • added @piplup/vite-plugin-cache-buster (2e16b0b)
  • bump package version for @piplup/vite-plugin-cache-buster (2b4f59c)
  • added active development message and docs boilerplate code (9f15e65)
  • reduced bundled size by optimizing babel configuration (c80327f)
  • use Object.hasOwn if supported and updated stories (e3c40bd)
  • added more components to x-date-pickers, storybook examples and improved transform prop type (224e1a8)
  • @piplup/use-local-storage: added @piplup/use-event-listener to external (336067f)
  • cache-buster: added @piplup/vite-plugin-cache-buster to README.md (5b1dbbd)
  • cache-buster: added a new hook useReleaseStatus (58aaa52)
  • rhf-adapters: added MuiXDatePickerElement (b44f873)
  • rhf-adapters: added MuiXDateFieldElement (83b25a8)
  • rhf-adapters: added date time components from @mui/x-date-pickers (33e396e)
  • rhf-adapters: added components from react-number-format (328cf22)
  • rhf-adapters: added mui-color-input, mui-file-input and mui-one-time-password-input (9f5a982)
  • rhf-adapters: added component from mui-tel-input (8564bdb)
  • rhf-adapters: added component from mui-chips-input (d18929c)
  • rhf-adpaters: added loading button from @mui/lab (bbadc7e)
  • rhf-adpaters: added more components from @mui/x-date-pickers (8b3ea87)
  • rhf-core: move imports for html components to @piplup/rhf-core/html (2f82b5d)
  • rhf-core: expose utilities functions and hooks used by rhf-core from @piplup/utils (a19f7c3)
  • vite-plugin-cache-buster: bump package version (39b9e76)

🩹 Fixes

  • package.json (de62877)
  • repository directory (7567eef)
  • package name (14c4eca)
  • update package json and replaced rmdir with rm (40260ac)
  • README.md (b66ac77)
  • cache-buster return type (755dc7d)
  • generate-release-id script and package.json peerDependencies (5bdce5a)
  • piplup-cache-buster cli command (2425794)
  • README.md (03b30ed)
  • children not visible when enabled is false (26d675b)
  • isAuthorized default values (ba43bc0)
  • isAuthorization role and permission match (4e8c778)
  • remove the component prop from HTMLFormHelperText and fix type import (10e2003)
  • rhf-adapters exports and updated README (6a0a8f8)
  • bugs for helper text rendering in FormHelperText and required prop in case of type checkbox (2663e2e)
  • nx config for publishing package and removed dist from package.json (942dc1f)
  • rollup.config.js for additionalEntryPoints (ef673b2)
  • fixed type warning for useMuiOtpInputAdapter (930ba53)
  • @piplup/use-local-storage: fixed license path (c30e2de)
  • cache-buster: fixed rollup.config.js for bin folder (943019e)
  • cache-buster: fixed status value in case enabled is false or window is undefined (3221197)
  • react-acl: must return false if no permission is matched (5d18b12)
  • react-acl: fixed README.md glob path (c283949)
  • rhf-adapters: return checked instead of value from useMuiSwitchAdapter (7db4dad)
  • rhf-adpaters: loading indictor when form is submitting (4080815)
  • rhf-core: fixed README.md glob path (f206bf4)
  • storybook: typo in adapter name (fed5fe7)
  • use-event-listener: fixed README.md glob path (39084ea)
  • use-local-storage: fixed usage example (de654f5)
  • use-local-storage: fixed README.md glob path (ae0c638)
  • vite-plugin-cache-buster: README.md usage instructions (87d9dcc)
  • vite-plugin-cache-buster: README.md options instructions (bbb2d4b)
  • vite-plugin-cache-buster: README.md fix import in options (8cf59e0)
  • vite-plugin-cache-buster: fixed README.md glob path (5a40153)

❤️ Thank You

  • Sadik Malik
  • sadik_malik
  • sadik-malik