Search code examples
javascriptreactjsredux

Resetting store to initialState is not working


I'm building a ReactJS app using Redux and redux-storage

I needed a button that can reset the store to its initial state, so I wrote my reducer like this:

const initialState={...}

export default function ProfilationSelectReducer (state=initialState, action){
 switch(action.type){
 ...
 case ProfilationSelectActionTypes.CLEAR_STORE:{

            return initialState
        }
 ...
 }
}

Now, this code WORKS in a page of the app but in other pages it looks like the initialState is changing (despite being a const) resulting in not clearing the state.

Any thought about this behavior? If more information is needed I'll be happy to share it with you


Solution

  • I figured out following this post: https://stackoverflow.com/a/35477308/8608574

    I changed the declaration of my initialState to a function. This is my code now:

    function getInitialState(){
     return{...}
    }
    
    export default function ProfilationSelectReducer (state=getInitialState(), action){
     switch(action.type){
     ...
     case ProfilationSelectActionTypes.CLEAR_STORE:{
            return getInitialState()
        }
     ...
    
     }
    }
    

    Thanks everybody for your help