mirror of
https://github.com/streamich/react-use.git
synced 2025-12-08 18:02:14 +00:00
781 B
781 B
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—localStoragekey to manage.initialValue— initial value to set, if value inlocalStorageis empty.raw— boolean, if set totrue, hook will not attempt to JSON serialize stored values.