Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
2 contributors

Users who have contributed to this file

@streamich @revskill10
35 lines (25 sloc) 781 Bytes

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);
  • key β€” localStorage 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.
You can’t perform that action at this time.