React Redux:Uncaught Error:期望reducer成为一个函数

时间:2017-05-21 17:17:20

标签: reactjs redux

我正在尝试在我的app.js文件中集成reducer,但是我一直收到以下错误: React Redux:Uncaught Error:期望reducer成为一个函数。 代码如下所示:

reducers / booksReducers.js文件

"use strict"

export function booksReducers(state={ books: [] }, action) {
  switch (action.type) {

    case 'POST_BOOK':
      // let books = state.books.concat(action.payload);
      // return {books};
      return { books: [...state.books, ...action.payload] }
      break;

    default:
      break;
  }
  return state;
}

reducers / index.js文件

"use strict"

import { combineReducers } from 'redux';

// HERE IMPORT REDUCERS TO BE COMBINED
import { booksReducers } from './booksReducers';

//HERE COMBINE THE REDUCERS
export default combineReducers({
  books: booksReducers
})

主app.js文件:

import { createStore } from 'redux';

import  { reducers } from './reducers/index';

const store = createStore(reducers);

store.subscribe(() => {
  console.log('current state is ', store.getState());
});

store.dispatch({
  type: 'POST_BOOK',
  payload: [
  {
    id: 1,
    title: 'Book Title',
    description: 'Book Description'
  },
  {
    id: 2,
    title: 'Second Book Title',
    description: 'Second Book Description'
  },
]
});

我不确定,我哪里出错了。谁能让我知道这里有什么问题?

由于

1 个答案:

答案 0 :(得分:2)

在您的主app.js文件中,替换

Output FSales ESales A 70 230 B 50 450

import { reducers } from './reducers/index';

与redcuers / index.js一样,import reducers from './reducers/index';默认导出。所以,你不需要combineReducers