# react-shared-storage

> ![License](https://img.shields.io/github/license/artmann/react-shared-storage?style=for-the-badge) ![Build Status](https://img.shields.io/github/workflow/status/artmann/react-shared-storage/CI?style=for-the-badge) ![Version](https://img.shields.io/npm/v/r

Latest version **2.0.4** (published 2022-10-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-shared-storage
pnpm add react-shared-storage
yarn add react-shared-storage
bun add react-shared-storage
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.4 |
| Published | 2022-10-22 |
| First published | 2022-10-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 289.9 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Christoffer Artmann |
| Maintainers | artmann |
| Keywords | front-end, hooks, localStorage, mobile, react, react hooks, typescript, state, web |

## Links

- npm: https://www.npmjs.com/package/react-shared-storage
- Repository: https://github.com/artmann/react-shared-storage
- npm.io page: https://npm.io/package/react-shared-storage

## Dependencies (3)

- [uuid](https://npm.io/package/uuid.md) ^9.0.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [broadcast-channel](https://npm.io/package/broadcast-channel.md) ^4.17.0

## 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

- 2.0.4 (latest) — 2022-10-22
- 2.0.3 — 2022-10-22
- 2.0.2 — 2022-10-22
- 2.0.1 — 2022-10-22
- 2.0.0 — 2022-10-22
- 1.0.2 — 2022-10-22
- 1.0.1 — 2022-10-11
- 1.0.0 — 2022-10-11

## README

# React Shared Storage

![License](https://img.shields.io/github/license/artmann/react-shared-storage?style=for-the-badge)
![Build Status](https://img.shields.io/github/workflow/status/artmann/react-shared-storage/CI?style=for-the-badge)
![Version](https://img.shields.io/npm/v/react-shared-storage?style=for-the-badge)

React Shared Storage is built to be a simple way for storing state in the browsers local storage. It will keep your state synced between different components and different windows and tabs.

![Demo](./assets/demo.gif)

## Installation

Install it with Yarn:

```shell
yarn add react-shared-storage
```

Or NPM:

```shell
npm i react-shared-storage
```

## Usage

The package exposes the `usePersistentState` hook which works similarly to set state. The biggest difference is that you will
also have to provide a key property which is used to identify the property between components and windows.

```tsx
import { usePersistentState } from 'react-shared-storage'

export function CounterComponent() {
  // The `counter` key will be used to identify the value.
  const [ counter, setCounter ] = usePersistentState('counter', 0)

  return (
    <div>
      <div>
        { counter }
      </div>
      <button onClick={ () => setCounter(counter + 1) }>
        Increase
      </button>
    </div>
  )
}

export function AnotherComponent() {
  // This component will also react to the `counter` value being changed.
  const [ counter, setCounter ] = usePersistentState('counter', 0)

  return (
    <div>
      <div>
        { counter }
      </div>
      <button onClick={ () => setCounter(counter + 1) }>
        Increase
      </button>
    </div>
  )
}

```

If you are want to save the state to local storage, but don't want it to sync between components and windows, you can disable the sync:

```tsx
usePersistentState('counter', 0, {
  shouldSync: false
})

```

The different options you can pass to the `usePersistentState` hook are:

|Name|Default Value|Description|
|---|---|---|
|**saveInterval**|`200`|Determines how often the state is saved to local storage.|
|**shouldSync**|`true`|Used to enable or disable syncing of state between windows.|


## Development

There is a demo app in the `demo/` directory that you can use for testing.
Start it with:

```shell
yarn demo
```

There is a linter:

```shell
yarn lint
```

and a test suite:

```shell
yarn test
```

## Contributing

1. Fork it (<https://github.com/your-github-user/test/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

## Contributors

- [Christoffer Artmann](https://github.com/your-github-user) - creator and maintainer

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