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

问题描述 投票:0回答:1

我正在尝试实现完全相同的行为如何重置 Redux 存储的状态?。除了我使用的是connected-react-router。我的配置如下所示:

减速机

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)
};
reactjs redux react-redux connected-react-router
1个回答
2
投票

这似乎是此软件包的“已知问题”。解决方案似乎是将状态设置为 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)
};

© www.soinside.com 2019 - 2024. All rights reserved.