reactjs - 如何使用 connected-react-router 重置 Redux 存储的状态?
问题描述
我正在尝试实现这种完全相同的行为如何重置 Redux 商店的状态?. 除了我使用的是连接反应路由器。我的配置如下所示:
减速器
export const rootReducer = history => combineReducers({
auth: auth.reducer,
router: connectRouter(history),
authentication: authenticationReducer,
users: usersReducer,
project: projectReducer,
domain: domainReducer,
test: testReducer
});
店铺
export const history = createBrowserHistory({ basename: '/metro' });
const sagaMiddleware = createSagaMiddleware();
const middleware = [
...getDefaultMiddleware({
immutableCheck: false,
serializableCheck: false,
thunk: true
}),
sagaMiddleware,
routerMiddleware(history)
];
const store = configureStore({
reducer: rootReducer(history),
middleware,
devTools: process.env.NODE_ENV !== "production",
enhancers: [reduxBatch]
});
我试过了,但不起作用:
const appReducer = (state, action, history) => combineReducers({
auth: auth.reducer,
router: connectRouter(history),
authentication: authenticationReducer,
users: usersReducer,
project: projectReducer,
domain: domainReducer,
test: testReducer
})
export const rootReducer = history => (state, action) => {
if (action.type === actionTypes.LOGOUT_SUCCESS) {
state = undefined;
}
return appReducer(state, action, history)
};
解决方案
这似乎是这个包的一个已知问题。解决方案似乎是将状态设置为一个initialState
值,而不是undefined
. 唯一需要注意的是,您不应该尝试重新初始化router
- 它应该默认执行此操作。
const appReducer = (state, action, history) => combineReducers({
auth: auth.reducer,
router: connectRouter(history),
authentication: authenticationReducer,
users: usersReducer,
project: projectReducer,
domain: domainReducer,
test: testReducer
})
const initialState = {
// Don't reset router here
auth: {},
authentication: {},
users: {},
project: {},
domain: {},
test: {}
}
export const rootReducer = history => (state, action) => {
if (action.type === actionTypes.LOGOUT_SUCCESS) {
state = initialState;
}
return appReducer(state, action, history)
};
推荐阅读
- wpf - WPF Dispatcher 线程挂起数小时,然后自动恢复
- python - 如何编写一个循环以将多个 txt 表写入 python 中自己的数据帧?
- python - 如何使用 .replace 减少命令以及创建函数中的问题
- big-o - 如何证明哪个渐近符号是有效的
- kotlin - Kotlin:在 when 中使用枚举
- amazon-web-services - 使用 Azure Devops 将 .NET 核心代码部署到 AWS lambda - 找不到项目路径
- android - 传入的通知使我的应用程序 Android 崩溃
- java - 如何向我的 Retry TestNG 方法添加睡眠?
- c# - 如何在内存中模拟仅在运行时持续的数据库?
- swift - 当它是存储属性时,可以在堆上分配 Swift 结构吗?