Redux reducer不更新状态对象

时间:2016-08-24 03:21:51

标签: javascript reactjs redux

我有以下代码,我正在尝试更新状态,但它无法正常工作。

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

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

我正在使用set但是当我在返回之前进行控制时它只打印原始对象。我做错了什么?

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;

1 个答案:

答案 0 :(得分:3)

state = state.set('isFetching', true); state = state.set('deparments', List(depts)); state = state.set('products', List(products)); 不会改变您的状态,它会返回原始对象的变异副本。

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

func bar(bts []byte) (a *User) {
  err := json.Unmarshal(bts, a) // It will crash
}