2.8.0 • Published 9 months ago

@piplup/use-local-storage v2.8.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@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.

1.4.1

9 months ago

1.4.0

9 months ago

2.7.0

9 months ago

2.8.0

9 months ago

1.2.0

9 months ago

1.3.0

9 months ago

1.1.0

10 months ago

1.0.3

10 months ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago