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.