我有一个异步reducer结构。在任何一个页面,我注入的页面与减速机:与反应路由器异步reducer似乎有多个商店?
export const injectReducer = (store, { key, reducer }) => {
// store.asyncReducers already initialized to {}
// makeRootReducer just returns combineReducers({...store.asyncReducers})
store.asyncReducers[key] = reducer
store.replaceReducer(makeRootReducer(store.asyncReducers))
}
我用react-router 3
和平原路线定义任何给定的网页上。我用require.ensure
的路线的异步处理中平原路线定义的getComponent
:
export default (store) => ({
path : 'counter',
getComponent (nextState, cb) {
require.ensure([], (require) => {
const Counter = require('./containers/Counter').default
const reducer = require('./modules/counter').default
injectReducer(store, { key: 'counter', reducer })
cb(null, Counter)
}, 'Counter')
}
})
我的问题是,如果我使用dispatch
其次browserHistory.push
,我希望,国家得到才去到新的页面更新。然而,会发生什么呢,似乎有2个独立的商店。例如,在页面之间导航时,尽管前一页的计数器位于同一个键上,但它的值似乎仍然保留。这里发生了什么???
样本repo我的问题。你可以git clone
,npm install
,npm start
并去localhost:3000/counter
。如果您点击Double (Async)
,它会将计数器加倍,然后进入/otherPage
。如果您再点击Half (Async)
它会将您带回/counter
。然而,计数器的值是从doubling
的值,而不是从halving
。另外,重要的是,拉起Redux DevTools
并在页面之间导航似乎显示之前所有数据的计数器更改。几乎好像整个商店被替换了,但是先前的价值被保留了下来。
这是怎么回事?