Skip to content

Instantly share code, notes, and snippets.

@karol-majewski
Last active May 5, 2018 00:38
Show Gist options
  • Select an option

  • Save karol-majewski/1afd4780dade985f703f1cf3e284aeb5 to your computer and use it in GitHub Desktop.

Select an option

Save karol-majewski/1afd4780dade985f703f1cf3e284aeb5 to your computer and use it in GitHub Desktop.
Equivalent for `handleActions` from the redux-actions` library.
import { AnyAction, Reducer } from 'redux';
/**
* Equivalent for `handleActions` from the redux-actions` library.
* @see https://redux.js.org/recipes/reducing-boilerplate#generating-reducers
*
* @template T Reducer shape.
*/
export function createReducer<T>(handlers: Record<string, Reducer<T>>, initialState: T): Reducer<T> {
return (state: T = initialState, action: AnyAction): T => {
if (action.type in handlers) {
return handlers[action.type](state, action);
}
return state;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment