# use-reducer-util

> A helper to create the reducer of useReducer

Latest version **1.0.4** (published 2021-08-30) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2021-08-30 |
| First published | 2021-08-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Lucas Floriani |
| Maintainers | lucasfloriani |
| Keywords | react, useReducer, util |

## Links

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

## 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.4 (latest) — 2021-08-30
- 1.0.3 — 2021-08-30
- 1.0.2 — 2021-08-30
- 1.0.1 — 2021-08-30
- 1.0.0 — 2021-08-30

## README

# use-reducer-util

It's a util that removes the boilerplate needed to create the reducer function of the hook useReducer

## How to install

```txt
yarn add use-reducer-util
npm install use-reducer-util
```

## How to use

### With useReducer hook

```ts
import { useReducer } from 'react'
import createReducer from 'use-reducer-util'

type InitialStateProps = {
  total: number
}

type ReducerActionProps =
  | { type: 'INCREASE' }
  | { type: 'DECREASE' }

// Our library to create the reducer function without the need of the switch case boilerplate
const reducer = createReducer<InitialStateProps, ReducerActionProps>({
  INCREASE: (state) => ({ ...state, total: state.total + 1 }),
  DECREASE: (state) => ({ ...state, total: state.total - 1 }),
})

const initialState: InitialStateProps = { total: 0 }

const useCounter = () => {
  const [state, dispatch] = useReducer(reducer, initialState)

  const increase = useCallback(() => dispatch({ type: 'INCREASE' }), [dispatch])
  const decrease = useCallback(() => dispatch({ type: 'DECREASE' }), [dispatch])

  return {
    state,
    actions: { increase, decrease }
  }
}
```

### With useImmerReducer hook

```ts
import { useImmerReducer } from 'use-immer'
import createReducer from 'use-reducer-util'

type InitialStateProps = {
  total: number
}

type ReducerActionProps =
  | { type: 'INCREASE' }
  | { type: 'DECREASE' }

// Third generics of createReducer is to inform if the code is using immer or not (UsingImmer),
// the default value is false
const reducer = createReducer<InitialStateProps, ReducerActionProps, true>({
  INCREASE: (state) => { state.total++ },
  DECREASE: (state) => { state.total-- },
})

const initialState: InitialStateProps = { total: 0 }

const useCounter = () => {
  const [state, dispatch] = useImmerReducer(reducer, initialState)

  const increase = useCallback(() => dispatch({ type: 'INCREASE' }), [dispatch])
  const decrease = useCallback(() => dispatch({ type: 'DECREASE' }), [dispatch])

  return {
    state,
    actions: { increase, decrease }
  }
}
```

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