使用combineReducer和一个不可变的reducer&常规减速器

时间:2016-09-14 15:48:52

标签: reactjs redux react-redux reducers

我试图将redux-auth集成到我的项目中,该项目在reducers中使用了不可变量。我的减速器目前不使用不可变量,因为我对环境不熟悉。 (我相信,我会在某些时候改变它们)

我想首先尝试使集成工作。我的应用程序运行,我可以在状态树中看到auth reducer;但是,它在控制台视图中显示Uncaught TypeError: e.get is not a function(anonymous function) @ combineReducers.js:107并且auth应用程序集成将无法正常工作。如果有人可以提供帮助,请告诉我!感谢。

export default combineReducers({
  routing: routerReducer,
  auth: authStateReducer,
  ....
});

combineReducers如下所示:

function combineReducers(reducers) {
  var finalReducers = isImmutable(reducers) ? reducers : _immutable2['default'].fromJS(reducers);
  finalReducers = finalReducers.filter(function (v) {
    return typeof v === 'function';
  });
  var sanityError;

  try {
    assertReducerSanity(finalReducers);
  } catch (e) {
    sanityError = e;
  }

  var defaultState = finalReducers.map(function (r) {
    return undefined;
  });

  return function combination(state, action) {
    if (state === undefined) state = defaultState;

    if (sanityError) {
      throw sanityError;
    }

    var dirty = false;
    var finalState = finalReducers.map(function (reducer, key) {
      **var oldState = state.get(key); // This is where the error gets triggered**
      var newState = reducer(oldState, action);
      dirty = dirty || oldState !== newState;
      if (typeof newState === 'undefined') {
        throw new Error(getErrorMessage(key, action));
      }
      return newState;
    });

    if (process.env.NODE_ENV !== 'production') {
      var warningMessage = getUnexpectedStateKeyWarningMessage(state, finalState, action);
      if (warningMessage) {
        console.error(warningMessage);
      }
    }

    return dirty ? finalState : state;
  };
}

module.exports = exports['default'];

将上述内容更改为:

import { authStateReducer } from "redux-auth";
import Immutable from "immutable";

const auth = (state = {}, action) => {
  return authStateReducer(Immutable.fromJS(state), action);
}

const appReducer = combineReducers({
  routing: routerReducer,
  auth,
  ...
});

似乎摆脱了错误信息&登录操作正常。但是auth reducer没有从动作中接收任何用户数据。

0 个答案:

没有答案