Skip to content

Instantly share code, notes, and snippets.

@gabemeola
Created May 1, 2019 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabemeola/c64a11a782e3ed0634013281578b46f9 to your computer and use it in GitHub Desktop.
Save gabemeola/c64a11a782e3ed0634013281578b46f9 to your computer and use it in GitHub Desktop.
useReducer with maps
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)
}
@gabemeola
Copy link
Author

Need to tighten the types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment