Skip to content

Instantly share code, notes, and snippets.

@brenopolanski
Forked from frankchang0125/rootReducer.js
Created January 7, 2019 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brenopolanski/ec61a622c56f41164ce102fbf0c2fa98 to your computer and use it in GitHub Desktop.
Save brenopolanski/ec61a622c56f41164ce102fbf0c2fa98 to your computer and use it in GitHub Desktop.
Reset all reducers back to their initial states when user logout
import {combineReducers} from 'redux';
import { LOGOUT } from '../common/constants';
import { UnauthorizedErrorReducer } from '../common/commonReducers';
import FirstReducer from './FirstReducer';
import SecondReducer from './SecondReducer';
import ThirdReducer from './ThirdReducer';
/* In order to reset all reducers back to their initial states when user logout,
* rewrite rootReducer to assign 'undefined' to state when logout
*
* If state passed to reducer is 'undefined', then the next state reducer returns
* will be its initial state instead; since we have assigned it as the default value
* of reducer's state parameter
* ex: const Reducer = (state = InitialState, action) => { ... }
*
* See: https://goo.gl/GSJ98M and combineReducers() source codes for details
*/
const appReducer = combineReducers({
unauthorized: UnauthorizedErrorReducer,
first: FirstReducer,
second: SecondReducer,
third: ThirdReducer,
});
export default rootReducer = (state, action) => {
if (action.type === LOGOUT) {
state = undefined;
}
return appReducer(state, action);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment