use-reducer-util
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

use-reducer-util

It's a util that removes the boilerplate needed to create the reducer function of the hook useReducer

How to install

yarn add use-reducer-util
npm install use-reducer-util

How to use

With useReducer hook

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

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

Dependencies (0)

    Dev Dependencies (1)

    Package Sidebar

    Install

    npm i use-reducer-util

    Weekly Downloads

    5

    Version

    1.0.4

    License

    MIT

    Unpacked Size

    5.03 kB

    Total Files

    6

    Last publish

    Collaborators

    • lucasfloriani