# use-remember-state

> [![github-version](https://badgen.net/github/release/PabloSzx/useRememberState)](https://github.com/PabloSzx/useRememberState) [![npm-version](https://badgen.net/npm/v/use-remember-state)](https://www.npmjs.com/package/use-remember-state) [![install-size]

Latest version **2.0.0** (published 2020-09-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-remember-state
pnpm add use-remember-state
yarn add use-remember-state
bun add use-remember-state
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2020-09-25 |
| First published | 2019-07-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 25 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | pablosz |

## Links

- npm: https://www.npmjs.com/package/use-remember-state
- Repository: https://github.com/PabloSzx/useRememberState
- npm.io page: https://npm.io/package/use-remember-state

## Recent versions

- 2.0.0 (latest) — 2020-09-25
- 1.0.1-0 (next) — 2019-07-06
- 1.2.0 — 2020-06-04
- 1.1.2 — 2019-09-21
- 1.1.1 — 2019-09-21
- 1.1.0 — 2019-09-21
- 1.0.4 — 2019-07-07
- 1.0.3 — 2019-07-07
- 1.0.2 — 2019-07-06
- 1.0.1 — 2019-07-06
- 1.0.0 — 2019-07-06
- 1.0.0-0 — 2019-07-06

## README

# useRememberState

[![github-version](https://badgen.net/github/release/PabloSzx/useRememberState)](https://github.com/PabloSzx/useRememberState)
[![npm-version](https://badgen.net/npm/v/use-remember-state)](https://www.npmjs.com/package/use-remember-state)
[![install-size](https://badgen.net/packagephobia/install/use-remember-state)](https://www.npmjs.com/package/use-remember-state)
[![publish-size](https://badgen.net/packagephobia/publish/use-remember-state)](https://www.npmjs.com/package/use-remember-state)

React Hook designed to extend the **useState** hook using **localStorage** as the persistance method on the browser, ready for **TypeScript** environments too.

## Installation

```bash
yarn add use-remember-state
```

or

```bash
npm install --save use-remember-state
```

## Why

When you use **useState** you need to give a _default value_, usually it's just used as a placeholder, but what if you want that value to be data you already put before?, the common solution is to use the _default value_ calling **localStorage.getItem(_name_)**, but of course you need to call **localStorage.setItem(_name_, _value_)** before, and this call should be handled everytime the value you want to save is changed. And you have to keep in mind that if you have **SSR** implemented, there is no **localStorage** on the server, so you need fallbacks, so clearly it just becomes a nightmare and tons of repetitive code.

All that problem is solved using the custom hook **useRememberState**

## Usage

The return values from the hook are exactly the same as **useState**, but the arguments should be first a **consistent name** which is going to be used as **key** for **localStorage**, and after that a **default value**, which is going to be used as fallback for the first time render or server side rendering.

```tsx
import React, { FunctionComponent } from "react";
import { useRememberState } from "use-remember-state";

const HelloWorld: FunctionComponent = () => {
  const [data, setData] = useRememberState("HelloWorldInput", "");

  return (
    <div>
      <p>This input remembers the input after every reload</p>
      <input
        value={data}
        onChange={({ target: { value } }) => {
          setData(value);
        }}
      />
    </div>
  );
};
```

Keep in mind that the name should be **unique** around the app.

For server side rendering the default value is only going to be used for the first render, but after the component is mounted on the browser, it will try to fetch again to **localStorage** looking for the data.

### SSR

Handling SSR correctly and efficiently with `localStorage` has it's problems, so in order to prevent setting state twice (therefore, rendering an extra time) and at the same time preventing the warning "**_useLayoutEffect does nothing on the server_**" when using SSR you **have to** specify an optional third argument:

```js
useRememberState(name, defaultValue, { SSR: true });
```

### Debounce

Accessing `localStorage` is computationally expensive, in order to improve performance all the calls to `localStorage` are [**debounced**](https://css-tricks.com/debouncing-throttling-explained-examples/#debounce), the default amount of delay is `500` _ms_, you can change it to any amount, or set it to `0` to make the calls synchronously.

```js
useRememberState(name, defaultValue, { debounceWait: 100 });
```

## License

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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