Skip to content

Instantly share code, notes, and snippets.

@trinadhkoya
Created February 14, 2019 07:29
Show Gist options
  • Save trinadhkoya/f28e2e3bded709ee05e30d6dfbf19a10 to your computer and use it in GitHub Desktop.
Save trinadhkoya/f28e2e3bded709ee05e30d6dfbf19a10 to your computer and use it in GitHub Desktop.
Reset to Initial State 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