react-use/docs/useSessionStorage.md
Andrew Joslin eca432a921 feat: add useSessionStorage hook
* Add useSessionStorage

* chore: uncomment session storage docs story
2018-11-06 21:45:57 +01:00

36 lines
799 B
Markdown

# `useSessionStorage`
React side-effect hook that manages a single `sessionStorage` key.
## Usage
```jsx
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
```js
useSessionStorage(key);
useSessionStorage(key, initialValue);
useSessionStorage(key, initialValue, raw);
```
- `key` &mdash; `sessionStorage` key to manage.
- `initialValue` &mdash; initial value to set, if value in `sessionStorage` is empty.
- `raw` &mdash; boolean, if set to `true`, hook will not attempt to JSON serialize stored values.