redux不可变存储可以包含不可变的存储

时间:2017-10-01 06:48:26

标签: javascript reactjs redux react-redux immutable.js

我正在使用带有immutablejs的redux。 我们想要使用内部使用redux但不使用immutablejs的插件。

  • 这是我创建app reducer的方法:
import { fromJS } from 'immutable';

import { combineReducers } from 'redux-immutable';
import { LOCATION_CHANGE } from 'react-router-redux';
import { reducer as formReducer } from 'redux-form/immutable';

// prebuilt admin-on-rest features
import { adminReducer, localeReducer } from 'admin-on-rest';

// Initial routing state
const routeInitialState = fromJS({
  location: null,
});

/**
 * Merge route into the global application state
 */
function routeReducer(state = routeInitialState, action) {
  switch (action.type) {
    /* istanbul ignore next */
    case LOCATION_CHANGE:
      return state.merge({
        location: action.payload,
      });
    default:
      return state;
  }
}

export default function createReducer(injectedReducers) {
  return combineReducers({
    route: routeReducer,
    admin: adminReducer, // <==== This reducer is not immutable
    // admin MUST be accessible using `(state) => state.admin` 
    // and NOT `(state) => state.get('admin')`
    locale: localeReducer(),
    form: formReducer,
    ...injectedReducers,
  });
}

这就是我启动商店的方式:

const store = createStore(
  createReducer(),
  fromJS({}),
  composeEnhancers(...enhancers)
);

只有admin store不应该是不可变的。

管理员是否有可能将该商店插入我们的immutablejs商店?

1 个答案:

答案 0 :(得分:0)

是的,您可以在不可变对象(商店)中包含普通的javascript对象/数组,但是您需要注意如何使用商店。您将无法执行fromJS(store),因为这将使整个商店不可变。