# use-sticky-reducer

> Custom React hooks to persist state on browser with the useState and useReducer patterns

Latest version **1.0.0** (published 2023-02-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-sticky-reducer
pnpm add use-sticky-reducer
yarn add use-sticky-reducer
bun add use-sticky-reducer
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2023-02-21 |
| First published | 2023-02-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 10.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Adam Dueck |
| Maintainers | adueck |
| Keywords | react, localStorage state, local storage react, storage, useState, persist state, persist storage, persistent storage, persistent state, useStickyState, useStickReducer, reducer |

## Links

- npm: https://www.npmjs.com/package/use-sticky-reducer
- Repository: https://github.com/adueck/use-sticky-reducer
- Homepage: https://github.com/adueck/use-sticky-reducer#readme
- Issues: https://github.com/adueck/use-sticky-reducer/issues
- npm.io page: https://npm.io/package/use-sticky-reducer

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

- 1.0.0 (latest) — 2023-02-21

## README

# use-sticky-reducer

Custom hooks to persist React state on browser with the [useState](https://beta.reactjs.org/reference/react/useState) and [useReducer](https://beta.reactjs.org/reference/react/useReducer) patterns

[Blog Post](https://adueck.github.io/blog/persisting-state-in-react-with-typescript-and-use-reducer/)

## Installation

In your React project folder, run:

```bash
yarn add use-sticky-reducer
```

```bash
npm install use-sticky-reducer
```

This is an ES2022 module and **requires Node 18 or later**.

## 💻 Example

```tsx
// App.tsx

import { useStickyReducer } from './useStickyReducer';

// these are the actions for modifying the state that are allowed
type Action = 'increment' | 'double' | 'reset';

// this is a function that takes the state and handles a given Action
// giving us a new state
function reducer(state: number, action: Action): number {
  if (dispatch === 'increment') {
    return state + 1;
  }
  if (dispatch === 'double') {
    return state * 2;
  }
  if (dispatch === 'reset') {
    return 0;
  }
}

export default function App() {
  const [count, dispatch] = useStickyReducer<number, Action>(
    reducer,
    0,
    'my-key',
    (saved: number) => saved - 2
  );
  return (
    <div>
      <div>Count: {count}</div>
      <button onClick={() => dispatch('increment')}>Increment</button>
      <button onClick={() => dispatch('double')}>Double</button>
      <button onClick={() => dispatch('reset')}>Reset</button>
    </div>
  );
}
```

## 📚 Usage

### useStickyReducer

```ts
import { useStickyReducer } from "use-sticky-reducer";

...

const [state, dispatch] = useStickyState(reducer, defaultValue, storageKey, initialization);
```

| Property       | Description                                                                                                                                                    |
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| reducer        | Reducer function                                                                                                                                               |
| defaultValue   | The default value used if storageKey was not previously set                                                                                                    |
| storageKey     | Unique string key used to persist data, using your browser's built in [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) API |
| initialization | OPTIONAL function to transform saved state on hook initialization                                                                                              |

### useStickyState

```ts
import { useStickyState } from "use-sticky-reducer";

...

const [state, setState] = useStickyState(defaultValue, storageKey, initialization);
```

| Property       | Description                                                                                                                                                    |
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| defaultValue   | The default value used if storageKey was not previously set                                                                                                    |
| storageKey     | Unique string key used to persist data, using your browser's built in [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) API |
| initialization | OPTIONAL function to transform saved state on hook initialization                                                                                              |

## With SSR

**This package is meant for client-side, browser use**. When used in SSR (server-side rendering) it will not fetch the saved state from localStorage, and default the the regular useState or useReducer behavior.

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