qweezz
qweezz

Reputation: 804

How to reset to initial state all props in Redux-React reducer except one?

Could you help me with following:

My initial state is:

const initialState: IMovieState = {
  movie: {
    filmId: 0,
    posterUrl: '',
    ....other props,
};

Reducer

export const movieReducer = (state = initialState, action: MovieActions): IMovieState => {
  switch (action.type) {
    case MovieActionTypes.FETCH_MOVIE: {
      return { ...state, movie: action.payload };
    }
    case MovieActionTypes.RESET_MOVIE_STATE: {
      return initialState;
    }
    default:
      return state;
  }
};

I need to reset all props to its initial state except property PosterUrl. How can I do that? Not sure if it matters for solving my problem but I use TS

Upvotes: 0

Views: 136

Answers (1)

Praveen Tamil
Praveen Tamil

Reputation: 1156

Please use the below line

case MovieActionTypes.RESET_MOVIE_STATE: {
  return {
      ...initialState, 
      movie: {
          ...initialState.movie, 
          posterUrl: state.movie.posterUrl
      }
  };
}

Upvotes: 1

Related Questions