react-use/docs/useLocalStorage.md
2018-11-02 07:45:10 +01:00

36 lines
781 B
Markdown

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