Skip to content

Instantly share code, notes, and snippets.

@elsangedy
Last active March 26, 2019 05:15
Show Gist options
  • Save elsangedy/f363deab37f91e305705744a533faa1b to your computer and use it in GitHub Desktop.
Save elsangedy/f363deab37f91e305705744a533faa1b to your computer and use it in GitHub Desktop.
import { useReducer as useReducerBase } from 'react'
export const useReducer = (reducer, initialState) => {
const [state, dispatch] = useReducerBase(reducer, initialState)
const customDispatch = (type, payload) => dispatch({ type, payload })
return [state, customDispatch, dispatch]
}
// usage
const myReducer = (state, { type, payload }) => {
switch(type) {
case 'add': return state + payload
default: state
}
}
const [state, dispatch] = useReducer(myReducer, 0)
dispatch('add', 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment