combineReducers将Immutable Map转换为普通的JS对象

时间:2016-08-12 21:01:24

标签: reactjs redux react-redux immutable.js

根据我的阅读,这应该不是问题(redux是v3.3)?我做错了什么?

rootReducer.js

import { combineReducers } from 'redux';
import { routeReducer } from 'react-router-redux';
import { reducer as reduxAsyncConnectReducer } from 'redux-async-connect';
import myReducer from './modules/myReducer';

export default combineReducers({
  routeReducer,
  reduxAsyncConnectReducer,
  myReducer,
});


myReducer.js

import { Map } from 'immutable';

const initialState = Map({});

export default (state = initialState, action = {}) => {
  switch (action.type) {
    default:
      // On the third run "@@INIT" the state gets converted from a Map to a plain JS object :(
      console.log('action:', action.type, 'state:', state);
      return state;
  }
};


输出

action: @@redux/INIT
state: Map {size: 0, _root: undefined, __ownerID: undefined, __hash: undefined, __altered: false}

action: @@redux/PROBE_UNKNOWN_ACTION_x.5.a.b.w.u.f.k.8.3.q.b.s.5.e.e.4.5.c.d.i
state: Map {size: 0, _root: undefined, __ownerID: undefined, __hash: undefined, __altered: false}

action: @@INIT
state: Object {}

1 个答案:

答案 0 :(得分:1)

你可以尝试不使用不可变映射作为初始状态...即只使用标准对象?似乎有一些问题/额外的工作,redux-async-connect支持immutable.js。有关详细信息,请参阅https://github.com/Rezonans/redux-async-connect/pull/45