V
V
Vanya Huk2018-05-08 21:14:21
React
Vanya Huk, 2018-05-08 21:14:21

How to return to the initial state of the reducer in the redux of a particular property?

There is an initial state

const initialState = {
    filter_models: {
      adventure_state: null,
      type_id: null,
    }
}

after certain actions, the type_id property has changed, how to roll back to the initial state of only the type_id property without affecting others?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yustas Alexu, 2018-05-08
@vanyahuk

export default function Reducer(state = initialState, action) {
  switch (action.type) {
    case RESET_TYPE_ID:
      return {
        ...state,
        filter_models: {
          ...state.filter_models,
          type_id: null
        }
      };

PS But in your case it's a bad idea, because resetting the value for each empty filter is not very efficient. Alternatively, you can completely rewrite filter_models each time the filter is changed, initially discarding filters with no values. PickBy from lodash can help with this:
const filters = _.pickBy(filterSet, _.identity);

M
Maxim, 2018-05-09
@maxfarseer

after certain actions, the type_id property has changed, how to roll back to the initial state of only the type_id property without affecting others?

I’ll add theories: above, Yustas Alexu wrote the code for you, it remains only to explain what is in it, you take everything that is in state and rewrite filter_models, in which, again, you take everything that was in state.filters_models and rewrite only one property .
Bottom line: you need to figure out how the operator works ..., create objects of different nesting in the sandbox, and based on the answer from Yuxus, try to change different fields without breaking the rest.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question