我在 ES6 的 require.ensure()
和 react-router
的 getComponent()
的帮助下动态添加组件>。一旦我为具有相同键(例如“A”)的每条记录打开单独的选项卡,我就会收到上述错误。我的动态加载是这样的:
require.ensure([], (require) => {
const req = require('./components/A).default
const reducer= require('./modules').default
injectReducer(store, {
key: 'A',
reducer
})
cb(null, req)
})
我的 createstore()
代码是这样的:
export const makeRootReducer = (asyncReducers) => {
return combineReducers({
B,
C,
...asyncReducers
})
}
export const injectReducer = (store, { key, reducer }) => {
if (Object.hasOwnProperty.call(store.asyncReducers, key))return
store.asyncReducers[key] = reducer
store.replaceReducer(makeRootReducer(store.asyncReducers))
}
现在的问题是,在打开浏览器选项卡时,除了第一次将警告显示为:
Unexpected key " A" found in previous state received by the reducer Expected to find one of the known reducer keys instead: "B", "C". Unexpected keys will be ignored.
如果我最初传递 Key 'A' combineReducers()
那么这个警告就会消失。但是这个 combineReducers()
是全局的,它将保持 reducers 的状态,所以我不想在初始加载时传递这个 Key。我该如何解决这个警告,请帮助我。
最佳答案
我今天遇到了这个问题,因为似乎没有答案我会给出对我有用的答案。
当我使用 createStore 函数设置 initialState 时出现此错误。我没有任何与初始状态树布局相匹配的缩减器,这意味着 redux 忽略了我的一些初始状态。这似乎只是树中第一级键的情况,但似乎很高兴让嵌套键通过。
因此,如果您得到这个,请确保您的 initialState 和 reducer 有一个匹配的树。这也在 the createStore documentation 中描述。 .
关于reactjs - 警告 : Unexpected key "A" found in previous state received by the reducer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41826224/