React side-effect hook that manages a single localStorage
key.
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>
);
};
useLocalStorage(key);
useLocalStorage(key, initialValue);
useLocalStorage(key, initialValue, raw);
key
βlocalStorage
key to manage.initialValue
β initial value to set, if value inlocalStorage
is empty.raw
β boolean, if set totrue
, hook will not attempt to JSON serialize stored values.