close

How to reset the state of a Redux store?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to reset the state of a Redux store? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to reset the state of a Redux store ?

  1. How to reset the state of a Redux store ?

    to reset the state of a Redux store In addition to Dan Abramov's answer, shouldn't we explicitly set action as action = {type: '@@INIT'} alongside state = undefined. With above action type, every reducer returns the initial state.

  2. How to reset the state of a Redux store ?

    to reset the state of a Redux store In addition to Dan Abramov's answer, shouldn't we explicitly set action as action = {type: '@@INIT'} alongside state = undefined. With above action type, every reducer returns the initial state.

Method 1

I’d like to point out that the accepted comment by Dan Abramov is correct except we experienced a strange issue when using the react-router-redux package along with this approach. Our fix was to not set state to undefined but rather still use the current routing reducer. So I would suggest implementing the solution below if you are using this package

const rootReducer = (state, action) => {
  if (action.type === 'USER_LOGOUT') {
    const { routing } = state
    state = { routing } 
  }
  return appReducer(state, action)
}

Method 2

In addition to Dan Abramov’s answer, shouldn’t we explicitly set action as action = {type: ‘@@INIT’} alongside state = undefined. With above action type, every reducer returns the initial state.

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment