Wakko
Wakko

Reputation: 79

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

Upvotes: 1

Views: 312

Answers (2)

Wakko
Wakko

Reputation: 79

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

Upvotes: 1

Femi Oni
Femi Oni

Reputation: 824

Those other pages are likely calling an action updating the store on load. use redux dev tool to see how your store is being updated. i can't recommend it enough, saves tone of work and time.

Upvotes: -1

Related Questions