# @jasonsbarr/use-storage

> Two simple React hooks for using browser storage

Latest version **1.1.1** (published 2021-10-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install @jasonsbarr/use-storage
pnpm add @jasonsbarr/use-storage
yarn add @jasonsbarr/use-storage
bun add @jasonsbarr/use-storage
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2021-10-21 |
| First published | 2021-10-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jason Barr |
| Maintainers | jasonsbarr |
| Keywords | localstorage, sessionstorage, react-hooks, react |

## Links

- npm: https://www.npmjs.com/package/@jasonsbarr/use-storage
- Repository: https://github.com/jasonsbarr/use-storage
- Homepage: https://github.com/jasonsbarr/use-storage#readme
- Issues: https://github.com/jasonsbarr/use-storage/issues
- npm.io page: https://npm.io/package/@jasonsbarr/use-storage

## Dependencies (1)

- [react](https://npm.io/package/react.md) ^17.0.2

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2021-10-21
- 1.1.0 — 2021-10-21
- 1.0.0 — 2021-10-21

## README

# useStorage

Two simple React hooks for using browser storage.

## Install

```
npm install @jasonsbarr/use-storage
```

## Usage

The only difference between the two hooks is whether you're using localStorage or sessionStorage. The APIs are exactly the same.

```js
import { useLocalStorage } from "@jasonsbarr/use-storage";
```

- `use(Local|Session)Storage(key[, defaultValue = {}, deserializer = null]);`

The initial call to `useLocalStorage` returns an array pair containing the JSON parsed item for `key`, or just a string if the value is a simple string, as well as a function to update the value for `key`. If there is no value in storage for `key`, you can pass in a default initial value as the second argument to `useLocalStorage`.

```js
const [value, setValue] = useLocalStorage("key", "default value");
```

Passing a new value to the setter function will update the value for `key` in storage and in your application.

```js
setValue("new value");
```

If the value you pass to the setter function is a string, it will pass through into browser storage untouched. This lets you use custom serializers as long as you convert the data to a string before passing it to the setter. Any non-string values will have `JSON.stringify` called on them before saving them to storage. You can pass a custom deserializer function as the third argument to `useLocalStorage`.

```js
const [value, setValue] = useLocalStorage(
  "key",
  "default value",
  customDeserializerFunction
);
```

---
_Source: https://npm.io/package/@jasonsbarr/use-storage · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
