1.0.2 • Published 5 years ago

react-enhanced-reducer-hook v1.0.2

Weekly downloads
114
License
MIT
Repository
github
Last release
5 years ago

useEnhancedReducer · GitHub license npm Build Status

useEnhancedReducer is an alternative to React.useReducer that accepts middlewares to do some cool things before and after dispatch.

Inspired by Redux Middleware.

3-second quick look

import useEnhancedReducer from 'react-enhanced-reducer-hook'

function App() {
  const middlewares = [
    ({ getState, dispatch }) => next => action => {
      // do something before dispatch...
      next(action)
       // do something after dispatch...
    }
  ]
  const [state, dispatch] = useEnhancedReducer(reducer, initialState, middlewares)
  // ...
}

Installation

npm install react-enhanced-reducer-hook --save

Real-world Usage

import React from 'react'
import useEnhancedReducer from 'react-enhanced-reducer-hook'
import thunkMiddleware from 'redux-thunk'

function logMiddleware({ getState }) {
  return next => action => {
    console.log('Prev State:', getState())
    console.log('Action:', action)
    next(action)
    console.log('Next State:', getState())
  }
}

function gaMiddleware({ getState }) {
  return next => action => {
    window.ga && window.ga('send', 'event', 'Action', action.type)
    next(action)
  }
}

function useAppReducer(reducer, inititalState) {
  return useEnhancedReducer(reducer, inititalState, [
    thunkMiddleware,
    logMiddleware,
    gaMiddleware
  ])
}

function counterReducer(state, action) {
  switch (action.type) {
    case '+1': return { count: state.count + 1 }
    case '-1': return { count: state.count - 1 }
    case '0': return { count: 0 }
    default: return state
  }
}

function resetCounterAfter1Second() {
  return dispatch => setTimeout(() => { dispatch({ type: '0' }) }, 1000)
}

function App() {
  const [state, dispatch] = useAppReducer(counterReducer, 0)
  return (
    <React.Fragment>
      <button onClick={() => dispatch({ type: '+1' })}>+</button>
      <button onClick={() => dispatch(resetCounterAfter1Second())}>Reset</button>
      <button onClick={() => dispatch({ type: '-1' })}>-</button>
      <br />
      Count: {state.count}
    </React.Fragment>
  )
}

Try the demo in codesanbox.

License

MIT