React side-effect hook that manages a single sessionStorage
key.
import {useSessionStorage} from 'react-use';
const Demo = () => {
const [value, setValue] = useSessionStorage('my-key', 'foo');
return (
<div>
<div>Value: {value}</div>
<button onClick={() => setValue('bar')}>bar</button>
<button onClick={() => setValue('baz')}>baz</button>
</div>
);
};
useSessionStorage(key);
useSessionStorage(key, initialValue);
useSessionStorage(key, initialValue, raw);
key
βsessionStorage
key to manage.initialValue
β initial value to set, if value insessionStorage
is empty.raw
β boolean, if set totrue
, hook will not attempt to JSON serialize stored values.