【问题标题】:Unable to change state when combining multiple reducers组合多个减速器时无法更改状态
【发布时间】:2019-08-22 21:23:59
【问题描述】:

我有两个减速器:

// 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 |   }

【问题讨论】:

  • 你安装了 redux-devtools 和浏览器扩展吗?

标签: redux next.js


【解决方案1】:

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

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

然后在项目根目录下的index.js

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

这给了我们所有的商店。

【讨论】:

  • 哦,抱歉,打错了。 mainReducer 是实际传递给 createStore 的。在代码中修复它。
猜你喜欢
  • 2017-02-26
  • 2019-02-14
  • 1970-01-01
  • 2021-07-18
  • 2019-12-09
  • 1970-01-01
  • 2019-11-23
  • 2021-10-13
相关资源
最近更新 更多