Skip to content

Instantly share code, notes, and snippets.

@ca057
Created September 8, 2020 08:57
Show Gist options
  • Save ca057/28f5fd94d7b081d4d6e5828816a6366f to your computer and use it in GitHub Desktop.
Save ca057/28f5fd94d7b081d4d6e5828816a6366f to your computer and use it in GitHub Desktop.
higher-order reducer to clear redux state on specific action
import { combineReducers, Action } from '@reduxjs/toolkit';
const withClearStateOnAction = <S = any>(clearStateAction: Action) => (
reducer: Reducer<S | undefined, Action>,
) => (state: S, action: Action) => {
if (clearStateAction.type === action.type) {
return reducer(undefined, clearStateAction);
}
return reducer(state, action);
};
// example usage
const logout = { type: 'APP/logout' }
const reducersWithClearOnLogout = withClearStateOnAction(logout)(combineReducers({}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment