# react-stable-state

> react-stable-state

Latest version **0.0.13** (published 2022-04-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-stable-state
pnpm add react-stable-state
yarn add react-stable-state
bun add react-stable-state
```

## Health

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

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.13 |
| Published | 2022-04-28 |
| First published | 2022-04-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 23.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | tomtsutom |
| Maintainers | tomtsutom |
| Keywords | react, react-hooks, useState |

## Links

- npm: https://www.npmjs.com/package/react-stable-state
- Homepage: https://github.com/tomtsutom/react-stable-state
- npm.io page: https://npm.io/package/react-stable-state

## Dependencies (1)

- [tslib](https://npm.io/package/tslib.md) ^2.3.1

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

- 0.0.13 (latest) — 2022-04-28
- 0.0.12 — 2022-04-13
- 0.0.11 — 2022-04-10
- 0.0.10 — 2022-04-10
- 0.0.9 — 2022-04-09
- 0.0.8 — 2022-04-09
- 0.0.7 — 2022-04-09
- 0.0.6 — 2022-04-09
- 0.0.5 — 2022-04-09
- 0.0.4 — 2022-04-09
- 0.0.3 — 2022-04-09
- 0.0.2 — 2022-04-09
- 0.0.1 — 2022-04-09

## README

# react-stable-state

[![test](https://github.com/tomtsutom/react-stable-state/actions/workflows/ci.yaml/badge.svg)](https://github.com/tomtsutom/react-stable-state/actions/workflows/ci.yaml)
[![Coverage Status](https://tomtsutom.github.io/react-stable-state/coverage-badges.svg)](https://tomtsutom.github.io/react-stable-state/lcov-report)

A simple React Hooks for stable useState.

This module was designed with the use case of saving data after a certain amount of time has elapsed after editing. By using `useStableState` instead of `useState`, the number of times to process can be greatly reduced.

You can check a demo page [here](https://tsutomu-ikeda.github.io/react-stable-state-demo).

## Installation

```bash
yarn add react-stable-state
```

or

```bash
npm i --save react-stable-state
```

## Example

Example of saving to Local Storage after the value is changed and a certain period of time has elapsed.

```tsx
import { useEffect } from "react";
import { useStableState } from "react-stable-state";

const App = () => {
  const [value, stableValue, setValue] = useStableState<string>({
    initialState: localStorage.getItem("key") || "",
  });

  useEffect(() => {
    console.log("stable value has changed!");
    localStorage.setItem("key", stableValue);
  }, [stableValue]);

  return (
    <div className="App" id="app">
      <input
        type="text"
        id="text-input"
        value={value}
        onChange={(e) => {
          e.preventDefault();
          setValue(e.target.value);
        }}
      ></input>
    </div>
  );
};
export default App;
```

You can also store values to server by calling the Fetch API in the `onStableStateChanged` callback function.
`onStableStateChanged` callback would not invoked when the state is changed by the `load` function

```tsx
import { useStableState } from "react-stable-state";

const App = () => {
  const [value, stableValue, setValue] = useStableState<string>({
    initialState: "",
    load: () =>
      fetch("/data_store").then((resp) => resp.text()),
    onStableStateChanged: () =>
      fetch("/data_store", {
        method: "POST",
        body: JSON.stringify({ value: stableValue }),
        headers: {
          "Content-Type": "application/json",
        },
      }).then(),
  });

  return (
    <div className="App" id="app">
      <input
        type="text"
        id="text-input"
        value={value}
        onChange={(e) => {
          e.preventDefault();
          setValue(e.target.value);
        }}
      ></input>
    </div>
  );
};
export default App;
```

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