Skip to content

Latest commit

 

History

History
35 lines (25 loc) · 799 Bytes

useSessionStorage.md

File metadata and controls

35 lines (25 loc) · 799 Bytes

useSessionStorage

React side-effect hook that manages a single sessionStorage key.

Usage

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>
  );
};

Reference

useSessionStorage(key);
useSessionStorage(key, initialValue);
useSessionStorage(key, initialValue, raw);
  • keysessionStorage key to manage.
  • initialValue — initial value to set, if value in sessionStorage is empty.
  • raw — boolean, if set to true, hook will not attempt to JSON serialize stored values.