admin管理员组

文章数量:1287260

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.

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.

Share Improve this question asked Feb 18, 2016 at 9:14 IljaIlja 46.5k103 gold badges289 silver badges527 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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 };

Look at Jiri Fornous solution instead, as this will mutate your data.

An even easier way is to just return INITIAL_STATE.

case ACTIONS.RESET_STATE:
      return INITIAL_STATE;

If you simply want to reset state pletely, just return the value of INITIAL_STATE:

export default function(state = INITIAL_STATE, action) {
  switch(action.type) {
    case ACTIONS.RESET_STATE:
      return {
                 search: {
                     listings: []
                 },
                 listings: []
             };
    default:
      return state;
  }
}

If you want to keep the INITIAL_STATE in a single place. Change the initial state creator to a function:

function get_INITIAL_STATE => {
  return { search: {
               listings: []
           },
           listings: []
         }
}

export default function(state = get_INITIAL_STATE(), action) {
  switch(action.type) {
    case ACTIONS.RESET_STATE:
      return get_INITIAL_STATE();
    default:
      return state;
  }
}

本文标签: javascriptHow to return redux state to initial stateStack Overflow