Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return redux state to initial state?

I'm having surprisingly difficult time figuring this out, essentially I'm trying to set state to initial state, so far I tried:

// -- Initial state ------------------------------------------------------------
const INITIAL_STATE = {
  search: {
    listings: []
  },
  listings: []
}

// -- Story structure for story editor -----------------------------------------
export default function(state = INITIAL_STATE, action) {
  switch(action.type) {
    case ACTIONS.RESET_STATE:
      return { ...state, INITIAL_STATE }
    default:
      return state;
  }
}

this just adds initial state to existing one


case ACTIONS.RESET_STATE:
      return { ...state, state = INITIAL_STATE }

this returns error


case ACTIONS.RESET_STATE:
      return { ...state, state: INITIAL_STATE }

this is adding initial state to existing one gain


case ACTIONS.RESET_STATE:
      return { ...state, search: { listings:[] }, listings: [] }

This works, but I start getting weird mutation errors.

like image 244
Ilja Avatar asked Jan 07 '23 15:01

Ilja


1 Answers

The proposed solution of Anders is right, but has potential problem with immutables. This generates always new object.

case ACTIONS.RESET_STATE:
    return { ...INITIAL_STATE };
like image 185
Jiri Fornous Avatar answered Jan 18 '23 08:01

Jiri Fornous