Dispatching UnknownAction to Reset State
Show how to dispatch an UnknownAction to trigger the default case in the reducers, effectively resetting the Redux store to its initial state.
const resetAction = { type: 'UNKNOWN_ACTION' };
Define an action that is not recognized by any reducer to trigger the default case.
store.dispatch(resetAction);
Dispatch the 'UNKNOWN_ACTION' to the Redux store, intending to reset the state.
function rootReducer(state = initialState, action) {
switch (action.type) {
// ...other actions
default:
return state;
}
}
A root reducer where the default case returns the current state. It must be modified to reset the state when receiving an unknown action.
function rootReducer(state = initialState, action) {
if (action.type === 'UNKNOWN_ACTION') return initialState;
// ...other switch cases
return state;
}
Modified root reducer that resets the state when it receives the 'UNKNOWN_ACTION'.