Created
September 8, 2020 08:57
-
-
Save ca057/28f5fd94d7b081d4d6e5828816a6366f to your computer and use it in GitHub Desktop.
higher-order reducer to clear redux state on specific action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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