2

let say I have my store with nested combineReducers as below

const rootReducer = () =>
  combineReducers({
    agent,
    customer: customerReducer
  });

const customerReducer = combineReducer({
  profile,
  account
})

In this case if I want to reset the full customer state to its initial state how can I do that?

Note:

"@reduxjs/toolkit": "^1.3.5",
"typescript": "^3.8.3"
y2bd
  • 5,783
  • 1
  • 22
  • 25
  • Hi Sunil, welcome to Stack Overflow. Could you add what you've tried so far in the above post? – y2bd May 13 '20 at 21:16

1 Answers1

2

Store Configuration:

Assuming this is how your store is configured using reduxjs/toolkit:

const store = configureStore({
  reducer: rootReducer,
  middleware: [
    ...getDefaultMiddleware(),
  ],
})

export default store

where the rootReducer is:

const rootReducer = combineReducers({
  agent: agentReducer,
  customer: customerReducer
});

And where customerReducer is nested, i.e. combination of two other reducers:

const customerReducer = combineReducer({
  profile,
  account
})

Answer:

To reset only customer state in your Redux Store, you need to create a wrapper reducer which will simply delegate the action to your customerReducer and whenever this wrapper reducer receives a CUSTOMER/RESET type of action, it resets the customer state:

const customerReducerWrapper = (state, action) => {
  if (action.type === 'CUSTOMER/RESET') {
    state = undefined
  }
  return customerReducer(state, action)
}

const rootReducer = combineReducers({
  agent: agentReducer,
  customer: customerReducerWrapper
});

See: How to reset redux state?

Community
  • 1
  • 1
Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87