Created
May 1, 2019 16:32
-
-
Save gabemeola/c64a11a782e3ed0634013281578b46f9 to your computer and use it in GitHub Desktop.
useReducer with maps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useReducer } from 'react'; | |
type Reducer<S = any> = (state: S, action: any) => S; | |
type Map = Record<string, Reducer> | |
export default function useReducerMap(map: Map, initialState: unknown) { | |
return useReducer((state, action) => { | |
const reducer = map[action.type]; | |
return reducer(state, action); | |
}, initialState) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need to tighten the types