Skip to content

Instantly share code, notes, and snippets.

@matthew-gerstman
Last active August 23, 2018 15:50
Show Gist options
  • Save matthew-gerstman/2094e725ecbda01342b8ec973f240f58 to your computer and use it in GitHub Desktop.
Save matthew-gerstman/2094e725ecbda01342b8ec973f240f58 to your computer and use it in GitHub Desktop.
// redux-utils/store.ts
import { combineReducers, createStore, Store } from "redux";
import { ReducerMap, StoreShape } from "./types";
let reducerMap: ReducerMap = {};
const store = createStore(combineReducers(reducerMap));
export function getStore() {
return store;
}
export function registerReducer(newReducers: ReducerMap): Store<StoreShape> {
reducerMap = { ...reducerMap, ...newReducers };
// We probably want to validate we're not replacing reducers that already
// exist here but that exercise is left up to the reader.
store.replaceReducer(combineReducers(reducerMap));
return store;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment