1

i have a two reducers one is for authentication and second is for fetching some list of data. when i log out from app, my authentication reducer gets reinitialize but i don't know how to reinitialize my second reducer as well at the time of logging out.

here is my combine reducers code:

export default combineReducers({
    auth: AuthReducer,
    patientReducer: PatientReducer,
});

then import my reducers in app.js:

import reducers from './reducers';


class App extends Component {

    render() {
        const store = createStore(reducers,{},applyMiddleware(ReduxThunk));
        return (
            <Provider store={store}>
                <RouterComponent />
            </Provider>
        );
    }
}

export default App;

please tell me if how to dispatch an action of log out so that both of my reducers get reinitialize/empty.

because whenever i log out and then log in from another account the app first renders the previous user's data list because my second reducer(patientReducer) doesn't get reinitialize/empty at the time of logging out and then re-renders the data list of the current logged in user.

Syed Habib
  • 11
  • 3

1 Answers1

1

You can have an INITIAL_STATE containing all your properties as empty/null. When the user signs out you can call an action which sends a type that is handled by all your needed reducers. In your reducers you return ...INITIAL_STATE.

Just to explain some points: Every time you dispatch an Action, all your Reducers receives that dispatch. However, only the Reducers that handles the same type returns the code you want to. Since you want to clear many reducers, you can just share the same type on all of them, so all will be able to handle a single action dispatch.

Hope it helps.

soutot
  • 3,531
  • 1
  • 18
  • 22
  • i already did what you suggest an it worked but is there any proper way to do it without defining the same type on all reducers? but Thanks @soutot :) – Syed Habib Sep 10 '17 at 20:18
  • I found this solution. It basically uses the same principle but in a very smart way. He uses 2 reducers, one to handle the logout dispatch and the other to handle the app dispatches. When he calls the logout type, one reducer returns the other sending undefined. Thus undefined triggers initial_state, it seems to work great. https://stackoverflow.com/questions/35622588/how-to-reset-the-state-of-a-redux-store/35641992#35641992 – soutot Sep 10 '17 at 20:45