# @hoeks/use-persisted-state

> The [hoeks](https://github.com/onursagir/hoeks) `usePersistedState` hook. Persist your state variable asynchronously. By default it uses IndexedDB. Made with TypeScript

Latest version **1.0.2** (published 2021-01-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install @hoeks/use-persisted-state
pnpm add @hoeks/use-persisted-state
yarn add @hoeks/use-persisted-state
bun add @hoeks/use-persisted-state
```

## 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.0.2 |
| Published | 2021-01-26 |
| First published | 2021-01-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 25.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | onursagir |

## Links

- npm: https://www.npmjs.com/package/@hoeks/use-persisted-state
- npm.io page: https://npm.io/package/@hoeks/use-persisted-state

## Dependencies (1)

- [idb-keyval](https://npm.io/package/idb-keyval.md) ^5.0.2

## Recent versions

- 1.0.2 (latest) — 2021-01-26
- 1.0.0 — 2021-01-26

## README

# @hoeks/use-persisted-state

The [hoeks](https://github.com/onursagir/hoeks) `usePersistedState` hook. Persist your state variable asynchronously. By default it uses IndexedDB. Made with TypeScript

## Installation

```bash
$ yarn add @hoeks/use-persisted-state
$ npm i --save @hoeks/use-persisted-state
```

## Usage

It's similar to the React.useState hook. The only difference is that you provide a second argument being the key which will be use to save the state in IndexedDB. It also returns an extra function to clear the state from IndexedDB and a 4th state indicating if the state has been hydrated

### Example

```javascript
import * as React from 'react';
import usePersistedState from '@hoeks/use-persisted-state';

const Example: React.FunctionComponent = () => {
  const [count, setCount, clear, isHydrated] = usePersistedState(1, 'example');

  if (isHydrated) return <p>Loading...</p>;

  return (
    <>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
      <button onClick={clear}>Clear me</button>
    </>
  );
};
```

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