Redux存储没有有效的reducer

kx5bkwkv  于 2023-06-06  发布在  其他
关注(0)|答案(9)|浏览(445)

还没有找到任何关于这个错误的东西:
“存储没有有效的缩减程序。确保传递给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;
}
tjjdgumg

tjjdgumg1#

import语句不正确。您可以将import { Foo } from 'bar'export Foo一起使用,* 或者 * 如果使用export default Foo导出,则使用import Foo from 'bar'
换句话说,将export default function FriendListReducer更改为export function FriendListReducer,或者将import语句从import { FriendListReducer }更改为import FriendListReducer

lg40wkob

lg40wkob2#

如果对象为空。

export  default  combineReducers({

 })

此错误将显示。

ut6juiuv

ut6juiuv3#

../reducers/reducers?这名字很奇怪
不管怎样,似乎../reducers/reducers不返回一个reducer,如果reducers是一个目录,在里面放一个index.js,导入每个reducer并创建一个根reducer

import FriendListReducer from "./FriendListReducer"

const rootReducer = combineReducers({
   friendList : FriendListReducer
})

export default rootReducer

**重要!!**您的州将有state.friendList。

我希望它能有所帮助

56lgkhnf

56lgkhnf4#

store.js
False

  • import { charactersSlice } from“./charactersSlice.js”;
    TRUE不使用{}
  • import charactersSlice from“./charactersSlice.js”;
oug3syen

oug3syen5#

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

2hh7jdfx

2hh7jdfx6#

在你的代码之上

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

qlckcl4x

qlckcl4x7#

我遇到这个错误是因为错误的importimport {taskslice} from“./TaskSlice”;

正道

然后,我做了导入,比如import tasksSlice from“./TaskSlice”;

gk7wooem

gk7wooem8#

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;
2cmtqfgy

2cmtqfgy9#

我也面临着这个问题。我所做的不是:

combineReducers(productReducer, cartReducer)

我说了:

combineReducers({ productReducer, cartReducer })

而且成功了它需要存储区的有效对象。

相关问题