use-localstorage2
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

use-localstorage2

NPM version NPM monthly download

React side-effect hook that manages a single localStorage key.

Installation

To install the stable version:

$ yarn add use-localstorage2

Usage

import {useLocalStorage} from 'react-use';

const Demo = () => {
  const [value, setValue, remove] = useLocalStorage('my-key', 'foo');

  return (
    <div>
      <div>Value: {value}</div>
      <button onClick={() => setValue('bar')}>bar</button>
      <button onClick={() => setValue('baz')}>baz</button>
      <button onClick={() => remove()}>Remove</button>
    </div>
  );
};

Reference

useLocalStorage(key);
useLocalStorage(key, initialValue);
useLocalStorage(key, initialValue, {raw: true});
useLocalStorage(key, initialValue, {
  raw: false,
  serializer: (value: T) => string,
  deserializer: (value: string) => T,
});
  • keylocalStorage key to manage.
  • initialValue — initial value to set, if value in localStorage is empty.
  • raw — boolean, if set to true, hook will not attempt to JSON serialize stored values.
  • serializer — custom serializer (defaults to JSON.stringify)
  • deserializer — custom deserializer (defaults to JSON.parse)

License

MIT

Package Sidebar

Install

npm i use-localstorage2

Weekly Downloads

1

Version

0.1.1

License

MIT

Unpacked Size

7.93 kB

Total Files

14

Last publish

Collaborators

  • nghiepit