组合多个异径管时无法更改状态

时间:2019-08-22 12:50:07

标签: redux next.js

我有两个减速器:

// reducer/reducers/credentialsErrorDisplay.js

export default function credentialsErrorDisplay(state = 'none', action) {
  switch (action.type) {
    case 'FLAGCREDENTIALSERROR':
      return { action.payload };
    default:
      return state
  }
}
// reducer/reducers/emailWarning.js

export default function emailWarning(state = 0, action) {
  switch (action.type) {
    case 'WARNFOREXISTINGEMAIL':
      return { action.payload };
    default:
      return state
  }
}

我正在尝试将它们组合成这样:

reducer/index.js

import { combineReducers } from 'redux';
import { createStore } from 'redux';
import credentialsErrorDisplay from './reducers';
import emailWarning from './reducers';

const mainReducer = combineReducers({
  credentialsErrorDisplay,
  emailWarning,
});

const makeStore = initialState => createStore(mainReducer, initialState);

export default makeStore;

但是在运行时,动作无法调用,并且状态保持不变。我想念什么吗?我要做的就是将一个巨型reducer分解成多个可管理的文件,以使代码更易于维护。但是似乎没有任何效果。

更新:刚发现并修复了几个导入错误,这些错误似乎起作用。现在,它会引发以下编译时错误:

./reducers/reducers/credentialsErrorDisplay.js
SyntaxError: /home/ubuntu/proost/web/reducers/reducers/credentialsErrorDisplay.js: Unexpected token, expected "," (4:21)

  2 |   switch (action.type) {
  3 |     case 'FLAGCREDENTIALSERROR':
> 4 |       return { action.payload };
    |                      ^
  5 |     default:
  6 |       return state
  7 |   }

1 个答案:

答案 0 :(得分:0)

我认为您要传递给createStore的是mainReducer,而不是reducer。在我当前的项目中,我们做了一些不同的操作,导入了所有的reducer,然后直接直接导出combineReducers

export default combineReducers({reducer1, reducer2, reducer3});

,然后在项目根目录的index.js中:

import rootReducer from './js/reducers/RootReducer';
...
const store = createStore(rootReducer);

这给了我们所有的商店。