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

Commit

Permalink
Fixed issue 'Unexpected key counter found in previous state received …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
shendepu committed Oct 24, 2016
1 parent c3abb24 commit d2355a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/store/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default (initialState = {}) => {
// Store Instantiation and HMR Setup
// ======================================================
const store = createStore(
makeRootReducer(),
makeRootReducer({}, initialState),
initialState,
compose(
applyMiddleware(...middleware),
Expand Down
13 changes: 11 additions & 2 deletions src/store/reducers.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { combineReducers } from 'redux'
import locationReducer from './location'

export const makeRootReducer = (asyncReducers) => {
export const makeRootReducer = (asyncReducers, initialState) => {
let missingReducers = { }
if (initialState !== undefined && typeof initialState === 'object') {
for (let key in initialState) {
if (!asyncReducers.hasOwnProperty(key)) {
missingReducers[key] = () => initialState[key]
}
}
}
return combineReducers({
location: locationReducer,
...asyncReducers
...asyncReducers,
...missingReducers
})
}

Expand Down

0 comments on commit d2355a4

Please sign in to comment.