Skip to content
This repository was archived by the owner on Sep 11, 2018. It is now read-only.

Commit d2355a4

Browse files
committed
Fixed issue 'Unexpected key counter found in previous state received by the reducer' when added window.__INITIAL_STATE of { counter: 1 }. The counter reducer is async injected, so there no reducer to key 'counter' when combineReducers in createStore intially. Therefore a warning from combineReducers shows in non-production mode, but the initial state of { counter: 1 } is removed in production mode
1 parent c3abb24 commit d2355a4

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/store/createStore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default (initialState = {}) => {
2525
// Store Instantiation and HMR Setup
2626
// ======================================================
2727
const store = createStore(
28-
makeRootReducer(),
28+
makeRootReducer({}, initialState),
2929
initialState,
3030
compose(
3131
applyMiddleware(...middleware),

src/store/reducers.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { combineReducers } from 'redux'
22
import locationReducer from './location'
33

4-
export const makeRootReducer = (asyncReducers) => {
4+
export const makeRootReducer = (asyncReducers, initialState) => {
5+
let missingReducers = { }
6+
if (initialState !== undefined && typeof initialState === 'object') {
7+
for (let key in initialState) {
8+
if (!asyncReducers.hasOwnProperty(key)) {
9+
missingReducers[key] = () => initialState[key]
10+
}
11+
}
12+
}
513
return combineReducers({
614
location: locationReducer,
7-
...asyncReducers
15+
...asyncReducers,
16+
...missingReducers
817
})
918
}
1019

0 commit comments

Comments
 (0)