diff --git a/index.d.ts b/index.d.ts index 50e8d018fa..2fc2af4c9e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -326,7 +326,9 @@ export interface Store { * * @param nextReducer The reducer for the store to use instead. */ - replaceReducer(nextReducer: Reducer): void + replaceReducer( + nextReducer: Reducer + ): Store /** * Interoperability point for observable/reactive libraries. diff --git a/src/createStore.js b/src/createStore.js index cef9a2ecae..57c57d7636 100644 --- a/src/createStore.js +++ b/src/createStore.js @@ -224,7 +224,7 @@ export default function createStore(reducer, preloadedState, enhancer) { * implement a hot reloading mechanism for Redux. * * @param {Function} nextReducer The reducer for the store to use instead. - * @returns {void} + * @returns {Store} The same store instance with a new reducer in place. */ function replaceReducer(nextReducer) { if (typeof nextReducer !== 'function') { @@ -238,6 +238,7 @@ export default function createStore(reducer, preloadedState, enhancer) { // will receive the previous state. This effectively populates // the new state tree with any relevant data from the old one. dispatch({ type: ActionTypes.REPLACE }) + return store } /** @@ -284,11 +285,12 @@ export default function createStore(reducer, preloadedState, enhancer) { // the initial state tree. dispatch({ type: ActionTypes.INIT }) - return { + const store = { dispatch, subscribe, getState, replaceReducer, [$$observable]: observable } + return store } diff --git a/test/replaceReducers.spec.ts b/test/replaceReducers.spec.ts new file mode 100644 index 0000000000..241171dbf8 --- /dev/null +++ b/test/replaceReducers.spec.ts @@ -0,0 +1,18 @@ +import { createStore, combineReducers } from '..' + +describe('replaceReducers test', () => { + it('returns the original store', () => { + const nextReducer = combineReducers({ + foo: (state = 1, _action) => state, + bar: (state = 2, _action) => state + }) + const store = createStore((state, action) => { + if (state === undefined) return 5 + return action + }) + + const nextStore = store.replaceReducer(nextReducer) + + expect(nextStore).toBe(store) + }) +})