首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用connected-react-router重置Redux存储的状态?

如何使用connected-react-router重置Redux存储的状态?
EN

Stack Overflow用户
提问于 2020-11-20 00:46:48
回答 1查看 316关注 0票数 1

我正在尝试获得完全相同的行为How to reset the state of a Redux store?。除了我使用的是连接的反应路由器。我的配置如下所示:

减速机

代码语言:javascript
复制
export const rootReducer = history => combineReducers({
    auth: auth.reducer,
    router: connectRouter(history),
    authentication: authenticationReducer,
    users: usersReducer,
    project: projectReducer,
    domain: domainReducer,
    test: testReducer
});

商店

代码语言:javascript
复制
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]
});

我试过了,但不起作用:

代码语言:javascript
复制
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)
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-20 00:57:46

这似乎是包含此包的known issue。解决方案似乎是将状态设置为initialState值,而不是undefined。唯一需要注意的是,您不应该尝试重新初始化router,默认情况下,-it应该这样做。

代码语言:javascript
复制
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)
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64916207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档