Connected-react-router: Resetting state in v5.0.1

Created on 9 Nov 2018  路  4Comments  路  Source: supasate/connected-react-router

How do you reset state now since connectRouter is passed inside combineReducer and a history object is passed when creating the root reducer?

Previous way:

const appReducer = combineReducers({
  general: GeneralReducer
});

const rootReducer = (state, action) => {
  if(action.type == 'RESET') {}

  return appReducer(state, action);
};

export default rootReducer;

Most helpful comment

@jjdp @svenadlung You can try following

const createAppReducer = (history) => combineReducers({
  general: GeneralReducer,
  router: connectRouter(history)
});

const initialState = {
    general: {},
    router: {}
};

const createRootReducer = (history) => (state, action) => {
  if (action.type == 'RESET') {
      return initialState;
  }

  return createAppReducer(history)(state, action);
};

export default createRootReducer;

All 4 comments

I am facing the same problem :(

@jjdp @svenadlung You can try following

const createAppReducer = (history) => combineReducers({
  general: GeneralReducer,
  router: connectRouter(history)
});

const initialState = {
    general: {},
    router: {}
};

const createRootReducer = (history) => (state, action) => {
  if (action.type == 'RESET') {
      return initialState;
  }

  return createAppReducer(history)(state, action);
};

export default createRootReducer;

Perfect, thx @sgal !

Hey folks, If anyone else comes across this issue, and is still having problems (specifically receiving the error Uncaught Could not find router reducer in state tree, it must be mounted under "router"), I fixed it by following @sgal's suggestion above but not setting anything for initialState.router.

If you manually set initiaState.router's value as shown above, you will receive this error.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bmueller-sykes picture bmueller-sykes  路  4Comments

alamchrstn picture alamchrstn  路  5Comments

AdrienLemaire picture AdrienLemaire  路  5Comments

alexseitsinger picture alexseitsinger  路  3Comments

tonoslav picture tonoslav  路  4Comments