Redux store 没有有效的 reducer

Redux store does not have a valid reducer

在这里找不到关于此错误的任何信息:

"Store does not have a valid reducer. Make sure the argument passed to combineReducers is an object whose values are 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;
}

您的顶级 reducer 函数似乎使用数组作为其默认值。 Redux 期望状态的最顶端是一个对象,而不是数组。尝试将数组放在该对象的特定键处,例如 { friendList : [] }.

../reducers/reducers ?这是一个奇怪的命名

反正好像../reducers/reducers没有return一个reducer,如果reducers是一个目录,在里面放一个index.js,导入每个reducer,创建一个root reducer

import FriendListReducer from "./FriendListReducer"

const rootReducer = combineReducers({
   friendList : FriendListReducer
})

export default rootReducer

Important!! you will have state.friendList in your state.

希望对你有所帮助

您的 import 陈述不正确。您可以将 import { Foo } from 'bar'export Foo 一起使用,如果您使用 export default Foo.

导出,则使用 import Foo from 'bar'

换句话说,将 export default function FriendListReducer 更改为 export function FriendListReducer,或将导入语句从 import { FriendListReducer } 更改为 import FriendListReducer

如果对象为空。

 export  default  combineReducers({

 })

将显示此错误。

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;

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

combineReducers(productReducer, cartReducer)

我做到了:

combineReducers({ productReducer, cartReducer })

它奏效了。 它需要一个有效的商店对象。

在你的代码上方

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 导出的

store.js

错误

  • 从“./charactersSlice.js”导入{charactersSlice};

正确 不使用{}

  • 从“./charactersSlice.js”导入字符切片;