V
V
Vanya Huk2018-08-29 18:42:48
redux
Vanya Huk, 2018-08-29 18:42:48

Is it possible to somehow get the first initialState of redux in react?

Is it possible to somehow get the first initialState of redux in react?
for example, I have a car filter and one property depends on another,
i.e. when i change type_transport_id i need to reset brand_id, automodel_id, year, and body_id to initial state, but when i change brand_id i need to reset automodel_id, year, and body_id etc.

brand_model : {

    type_transport_id: null,

                brand_id: null,

    automodel_id: null,

    year: null,

    body_id: null,
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-08-29
@vanyahuk

const initialState = {
  type_transport_id: null,
  brand_id: null,
  automodel_id: null,
  year: null,
  body_id: null,
};

export default function model(state = initialState, action) {
  const { type, payload } = action;

  switch(type) {
    case SET_TYPE:
      return {
        ...initialState,
        type_transport_id: payload,
      };
 
    case SET_BRAND:
      return {
        ...state,
        brand_id: payload,
        automodel_id: null,
        year: null,
        body_id: null,
      };

    default:
      return state;
  }
}

Or the SET_BRAND case can also be represented as follows:
case SET_BRAND:
  return {
    ...initialState,
    type_transport_id: state.type_transport_id,
    brand_id: payload,
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question