reactjs存储reducer未正确实现

时间:2017-08-15 22:53:41

标签: reactjs

我在我的reactjs应用程序中遇到了这个错误:

Store does not have a valid reducer. Make sure the argument passed to combineReducers is an object whose values are reducers.

我的商店看起来像这样:

import {applyMiddleware, combineReducers, createStore} from "redux";
import thunk from "redux-thunk";

const store = createStore(
    combineReducers({}),
    applyMiddleware(thunk)
);

export default store;

它应该如何正确?

2 个答案:

答案 0 :(得分:1)

在将减速器添加到combineReducers之前,您将收到此错误,请参阅下面的示例。

反减速器'/减速器/计数器'

export default (state = 0, action) => {
  switch (action.type) {
    case 'INCREMENT':
     return state + 1;
   case 'DECREMENT':
     return state - 1;
   default:
     return state;
  }
};

商品

import {applyMiddleware, combineReducers, createStore} from "redux";
import thunk from "redux-thunk";
import counter from '/reducers/counter';

const store = createStore(
    combineReducers({counter}),
    applyMiddleware(thunk)
);

export default store;

答案 1 :(得分:1)

如果您不打算使用reducer分解,那么为什么使用combineReducers?只需将以下函数定义为您的唯一缩减器并将其传递给createStore()。

(state = {}, action) => {
  switch (action.type) {
    default:
      return state
  }
}

如果你只想让它起作用,或者只是以下内容:

(state = {}, action) => state