use-local-storage

v4.7.0arrow_drop_down
v4.7.0
STATUS
Passing
DOWNLOADS
165
VISIBILITY
Public
PUBLISHED
6 years ago
SIZE
N/A
1 contributor
Install use-local-storage as a package?
Copied
npm i @bit/giladshoham.react-hooks.use.use-local-storage
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.cloud
Component Example
React
React
Vue
Angular
React Native
Add dependency... help_outline
Just
import
any of the 1 million components
and packages in Bit or NPM to the example.
import Button from '@bit/grommet.grommet.button';
import Lodash from 'lodash';
toggle layout
chevron_left
chevron_right

useLocalStorage

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

Usage

import {useLocalStorage} from 'react-use';

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

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

Reference

useLocalStorage(key);
useLocalStorage(key, initialValue);
useLocalStorage(key, initialValue, raw);
  • keylocalStorage key to manage.
  • initialValue — initial value to set, if value in localStorage is empty.
  • raq — boolean, if set to true, hook will not attempt to JSON serialize stored values.
Help and resources