# @talves/use-web-storage

> React hook (useWebStorage)

Latest version **0.7.1** (published 2021-03-10) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.7.1 |
| Published | 2021-03-10 |
| First published | 2021-01-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 19.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | talves |
| Maintainers | talves |

## Links

- npm: https://www.npmjs.com/package/@talves/use-web-storage
- Repository: https://github.com/talves/react-use-hooks/packages/@talves/use-web-storage
- npm.io page: https://npm.io/package/@talves/use-web-storage

## Dependencies (1)

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

## Recent versions

- 0.7.1 (latest) — 2021-03-10
- 0.7.0 — 2021-02-19
- 0.6.0 — 2021-02-12
- 0.5.2 — 2021-02-09
- 0.5.1 — 2021-02-06
- 0.5.0 — 2021-02-03
- 0.4.0 — 2021-02-02
- 0.3.0 — 2021-02-01
- 0.2.0 — 2021-02-01
- 0.1.1 — 2021-01-31
- 0.1.0 — 2021-01-28

## README

<div align="center">
<h1>@talves/use-web-storage</h1>
<p>React hook (useWebStorage)</p>
</div>

<hr />

See [reference][reference] to get aquainted with the Web Storage API

The two mechanisms within Web Storage are as follows:

- `Window.sessionStorage`
- `Window.localStorage`

## Installation

This module is distributed via [npm][npm] which is bundled with [node][node] and
should be installed as one of your project's `dependencies`:

```
npm install --save @talves/use-web-storage
```

OR

```
yarn add @talves/use-web-storage
```

## Usage

```javascript
import { useWebStorage } from "@talves/use-web-storage";

export default (props) => {
  const [storage, setStorage] = useWebStorage(
    "test-data",
    { site: "tony.alves.dev" }
    // { type: "sessionStorage" } // default is "localStorage"
  );

  const handleChangeEvent = (e) => {
    setStorage({ site: e.target.value });
  };

  return (
    <div>
      <input
        onChange={handleChangeEvent}
        defaultValue={storage ? storage.site : ""}
      />
      {storage && <pre>{JSON.stringify(storage, null, 2)}</pre>}
    </div>
  );
};
```

NOTE: This is a `module` package library for react with jsx. The choice is to resolve the export of main to `src/index.js`. There is a commonjs version in `dist/cjs/index.js` if someone needed.

**IMPORTANT** [(Must Read)][storageevent]: Tracking Changes from other pages/settings.

- `window.addEventListener('storage', function(e) {})` does not track changes on the same page. The reason you use the hook for page state.
- `StorageEvent` is fired whenever a change is made to the Storage object only for `localStorage` changes on different pages of the same domain. (Note that this event is not fired for `sessionStorage` changes)

[reference]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API
[storageevent]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#responding_to_storage_changes_with_the_storageevent
[mdn-example]: https://github.com/mdn/dom-examples/tree/master/web-storage
[npm]: https://www.npmjs.com/
[node]: https://nodejs.org/

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