V
V
Vanya Huk2018-04-11 19:02:42
React
Vanya Huk, 2018-04-11 19:02:42

Why is the second reducer nullified when updating the 1st reducer?

when updating one reducer, for some reason, the properties of the second are reset

import { combineReducers } from 'redux';
import filterParams from './filterParams';

import filterAjaxParams from './filterAjaxParams';

import api from './api';

export default combineReducers({

filterParams: filterParams,

filterAjaxParams: filterAjaxParams,

})

when updating filterAjaxParams properties of filterParams are reset
const filterAjaxParams = (state = [], action) => {



switch (action.type) {

    case "UPDATE_AJAX_PARAMS" :

        return Object.assign({}, state, action.payload);

    default: return{

            types: [],


            brands: [],


            models: [],


            years: [],


            regions: [],


        }
}
}

export default filterAjaxParams;

const filterParams = (state = [], action) => {


  switch( action.type ){

    case "UPDATE_CHANGED":
      return Object.assign({}, state, action.payload );


    case "UPDATE_ADVENTURE_STATE":

      return Object.assign({}, state, action.payload );



    default : return {

      filter_models: {

        adventure_state: null,

        type_id: null,

        bodies: [],

        brands: [],

        models: [],

        countries: [],

        ignore_countries: null,

        ignore_brands: null,

        ignore_models: null


      },

      additionally_filter: {

        years: {

          from: null,

          to: null,
        },

        price: {

          from: null,

          to:   null
        },

        auction_possible: null,

        exchange_car: null,

        inexpensive: null,

        broken: null,

        after_an_accident: null,

        currency_id: null

      },

      region: {

        regions: [],

        cities: [],

        search_neighborns: null
      },

      specifications:  {

        fuels: [],

        transmissions: [],

        count_doors: {

          from: null,

          to: null

        },

        seats_number: {

          from: null,

          to: null

        },

        volume: {

          from: null,

          to: null

        },

        power: {

          from: null,

          to: null

        },

        race: {

          from: null,

          to: null
        },

        colors: {

          items: [],

          metallic: null
        },


        options: []

      },


      page: null,

      sort: null,

      period: null

    };
  }

}

export default filterParams;

5ace30170c003907844761.png
i update filterAjaxParams.years
5ace3169c05a6909504448.png
but it resets filterParams properties
5ace3189d126f835345089.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
monochromer, 2018-04-19
@monochromer

In the reducer, you need to return the already existing state by default

function reducer(state = initialState, action) {
  switch (action.type) {
     // ...
    default:
      return state
  }
}

Format code before posting a question

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question