1 contributor
Like
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
and packages in Bit or NPM to the example. any of the 1 million componentstoggle 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);
key
—localStorage
key to manage.initialValue
— initial value to set, if value inlocalStorage
is empty.raq
— boolean, if set totrue
, hook will not attempt to JSON serialize stored values.