kobe kobe - 3 months ago 30
React JSX Question

Redux reducer not updating state object

I have the following code, and I am trying to update the state but it is not working.

import Immutable from 'immutable';
import _un from 'underscore';
import { List, Map } from 'immutable';

const defaultState = Map({
isFetching:true,
deparments: List(),
products:List(),
breadcrumb:List()
})


I am using set but when I console before return it just prints the original object. What am I doing wrong?

switch(action.type) {
case 'GET_GALLERY_DATA':

//console.log("-- api success handler--");
//console.log(action);

var depts = getGalleryParsedData(action.res.data);
var products = getProducts(action.res.data);
var breadcrumb = getBreadcrumbs(action.res.data);

state.set('isFetching', true);
state.set('deparments', List(depts))
state.set('products', List(products))
//state.set('breadcrumb', List(breadcrumb))

console.log("---state----");
console.log(state);
return state;

Answer

Immutable.js does not mutate your state, it returns a mutated copy of the original object.

state = state.set('isFetching', true);
state = state.set('deparments', List(depts));
state = state.set('products', List(products));

or

state = state
  .set('isFetching', true)
  .set('deparments', List(depts))
  .set('products', List(products));