Redux商店没有有效的减速器

时间:2016-04-04 16:22:51

标签: reactjs redux reducers

此处未能找到有关此错误的任何内容:

  

“Store没有有效的reducer。请确保传递给combineReducers的参数是一个值为reducers的对象。”

我的减速机

export default function FriendListReducer(state = {friends : []}, action) {
  switch (action.type) {
    case 'ADD_FRIEND':
      return [
        { friends : action.payload.friend }, ...state.friends
      ]     
    default:
      return state;
  }
  return state;
}

import { combineReducers } from 'redux';
import { FriendListReducer } from './FriendListReducer';

const rootReducer = combineReducers({
  friends: FriendListReducer
});
export default rootReducer;

我的商店配置

import { applyMiddleware, createStore } from 'redux';
import thunkMiddleware from 'redux-thunk';
import createLogger from 'redux-logger';
import rootReducer from '../reducers/reducers';

export default function configureStore(initialState = { friends: [] }) {
  const logger = createLogger({
    collapsed: true,
    predicate: () =>
    process.env.NODE_ENV === `development`, // eslint-disable-line no-unused-vars
  });

  const middleware = applyMiddleware(thunkMiddleware, logger);

  const store = middleware(createStore)(rootReducer, initialState);

  if (module.hot) {
    // Enable Webpack hot module replacement for reducers
    module.hot.accept('../reducers/reducers', () => {
      const nextRootReducer = require('../reducers/reducers').default;
      store.replaceReducer(nextRootReducer);
    });
  }

  return store;
}

7 个答案:

答案 0 :(得分:26)

Your import statement is incorrect. Either you use import { Foo } from 'bar' together with export Foo, or use import Foo from 'bar' if you export with export default Foo.

In other words, change either export default function FriendListReducer to export function FriendListReducer, or change the import statement from import { FriendListReducer } to import FriendListReducer.

答案 1 :(得分:14)

如果对象为空。

 export  default  combineReducers({

 })

此错误将显示。

答案 2 :(得分:2)

../reducers/reducers ? it's a strange naming

Anyway, it seems ../reducers/reducers doesn't return a reducer, if reducers is a directory, put a index.js inside, import each reducer and create a root reducer

import FriendListReducer from "./FriendListReducer"

const rootReducer = combineReducers({
   friendList : FriendListReducer
})

export default rootReducer

you will have state.friendList in your state.

I hope it will help

答案 3 :(得分:0)

It looks like your top-level reducer function is using an array as its default value. Redux expects that the very top of your state will be an object, not an array. Try putting the array at a particular key in that object, like { friendList : [] }.

答案 4 :(得分:0)

我也遇到了问题。 我所做的不是:

combineReducers(productReducer, cartReducer)

我做到了:

combineReducers({ productReducer, cartReducer })

成功了。 它期望商店的有效对象。

答案 5 :(得分:0)

代码上方

import { FriendListReducer } from './FriendListReducer';

const rootReducer = combineReducers({
  friends: FriendListReducer
});
export default rootReducer;

代替import { FriendListReducer } from './FriendListReducer';

只说import FriendListReducer from './FriendListReducer';

因为FriendListReducer是用export default FriendListReducer而不是export const FriendListReducer导出的

答案 6 :(得分:-1)

Please check your combine reducer file It's empty......

you have forgot bind reducer here

import {combineReducers, createStore} from 'redux'
import listDataReducer from '../reducers/ListDataReducer'

const rootReducer = combineReducers({
    listDataReducer,         //  Please add your reducer here
});

export default rootReducer;