Skip to content

Commit 5b4728e

Browse files
committed
Add an explicit private action type for replacing reducers
1 parent 7bea62a commit 5b4728e

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

src/combineReducers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function getUnexpectedStateShapeWarningMessage(inputState, reducers, action, une
4444
unexpectedKeyCache[key] = true
4545
})
4646

47+
if (action && action.type == ActionTypes.REPLACE) return
48+
4749
if (unexpectedKeys.length > 0) {
4850
return (
4951
`Unexpected ${unexpectedKeys.length > 1 ? 'keys' : 'key'} ` +

src/createStore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export default function createStore(reducer, preloadedState, enhancer) {
213213
}
214214

215215
currentReducer = nextReducer
216-
dispatch({ type: ActionTypes.INIT })
216+
dispatch({ type: ActionTypes.REPLACE })
217217
}
218218

219219
/**

src/utils/actionTypes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* Do not reference these action types directly in your code.
66
*/
77
var ActionTypes = {
8-
INIT: '@@redux/INIT'
8+
INIT: '@@redux/INIT',
9+
REPLACE: '@@redux/REPLACE'
910
}
1011

1112
export default ActionTypes

test/createStore.spec.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { createStore, combineReducers } from '../src/index'
2-
import {
3-
addTodo,
4-
dispatchInMiddle,
2+
import {
3+
addTodo,
4+
dispatchInMiddle,
55
getStateInMiddle,
66
subscribeInMiddle,
77
unsubscribeInMiddle,
8-
throwError,
9-
unknownAction
8+
throwError,
9+
unknownAction
1010
} from './helpers/actionCreators'
1111
import * as reducers from './helpers/reducers'
1212
import * as Rx from 'rxjs'
@@ -767,4 +767,30 @@ describe('createStore', () => {
767767
expect(results).toEqual([ { foo: 0, bar: 0, fromRx: true }, { foo: 1, bar: 0, fromRx: true } ])
768768
})
769769
})
770+
771+
it('does not log an error if parts of the current state will be ignored by a nextReducer using combineReducers', () => {
772+
const originalConsoleError = console.error
773+
console.error = jest.fn()
774+
775+
const store = createStore(
776+
combineReducers({
777+
x: (s=0, a) => s,
778+
y: combineReducers({
779+
z: (s=0, a) => s,
780+
w: (s=0, a) => s,
781+
}),
782+
})
783+
)
784+
785+
store.replaceReducer(
786+
combineReducers({
787+
y: combineReducers({
788+
z: (s=0, a) => s,
789+
}),
790+
})
791+
)
792+
793+
expect(console.error.mock.calls.length).toBe(0)
794+
console.error = originalConsoleError
795+
})
770796
})

0 commit comments

Comments
 (0)