Redux Toolkit :Error: "reducer" is a required argument, and must be a function or an object of functions that can be passed to combineReducers

Redux Toolkit :Error: "reducer" is a required argument, and must be a function or an object of functions that can be passed to combineReducers

使用这段代码我没有错误。

import {combineReducers, configureStore} from '@reduxjs/toolkit';
import {menuReducer as menu} from './menu';
import {cartReducer as cart} from './shoppingCart';
import {optionsReducer as options} from './optionsItem';
import {homeReducer as home} from './home';
import {rewardReducer as reward} from './reward';
import {ordersReducer as orders} from './orders';
import {authReducer as auth} from './auth';
import giftCardReducer from '../store/giftCard';
import paymentMethodReducer from './paymentMethod';
import paymentToken from "./paymentToken";
import qRCode from "./qRCode";
import orderHistory from './orderHistory';
import orderDetail from './orderDetail';
import showCompletedOrder from './showCompletedOrder';
import paymentOptionsSummary from './paymentOptionsSummary';
import usersUpdate from './usersUpdate';
import referAFriend from './referAFriend';
import resetPassword from './resetPassword';

const reducer = combineReducers({
  menu,
  auth,
  giftCardReducer,
  paymentMethodReducer,
  cart,
  options,
  orders,
  home,
  reward,
  paymentToken,
  qRCode,
  orderHistory,
  orderDetail,
  showCompletedOrder,
  paymentOptionsSummary,
  usersUpdate,
  referAFriend,
  resetPassword
});
export {menuActions} from './menu';
export {cartActions} from './shoppingCart';
export {optionsActions} from './optionsItem';
export {ordersActions} from './orders';
export {authActions} from './auth';
export {homeActions} from './home';
export {rewardActions} from './reward';

export type rootState = ReturnType<typeof reducer>;

export default configureStore({
  reducer:reducer
});


const rootReducer = (state, action) => {
  if (action.type === 'auth/logout') { // check for action type 
    state = undefined;
  }
  return reducer(state, action);
};

如果我用下面的代码更改此代码,我会收到错误提示。

import {combineReducers, configureStore} from '@reduxjs/toolkit';
import {menuReducer as menu} from './menu';
import {cartReducer as cart} from './shoppingCart';
import {optionsReducer as options} from './optionsItem';
import {homeReducer as home} from './home';
import {rewardReducer as reward} from './reward';
import {ordersReducer as orders} from './orders';
import {authReducer as auth} from './auth';
import giftCardReducer from '../store/giftCard';
import paymentMethodReducer from './paymentMethod';
import paymentToken from "./paymentToken";
import qRCode from "./qRCode";
import orderHistory from './orderHistory';
import orderDetail from './orderDetail';
import showCompletedOrder from './showCompletedOrder';
import paymentOptionsSummary from './paymentOptionsSummary';
import usersUpdate from './usersUpdate';
import referAFriend from './referAFriend';
import resetPassword from './resetPassword';

const reducer = combineReducers({
  menu,
  auth,
  giftCardReducer,
  paymentMethodReducer,
  cart,
  options,
  orders,
  home,
  reward,
  paymentToken,
  qRCode,
  orderHistory,
  orderDetail,
  showCompletedOrder,
  paymentOptionsSummary,
  usersUpdate,
  referAFriend,
  resetPassword
});
export {menuActions} from './menu';
export {cartActions} from './shoppingCart';
export {optionsActions} from './optionsItem';
export {ordersActions} from './orders';
export {authActions} from './auth';
export {homeActions} from './home';
export {rewardActions} from './reward';

export type rootState = ReturnType<typeof reducer>;

export default configureStore({
  reducer:rootReducer
});


const rootReducer = (state, action) => {
  if (action.type === 'auth/logout') { // check for action type 
    state = undefined;
  }
  return reducer(state, action);
};

请提前帮助me.Thanks。 //////////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////////////////////////////// /////////////////////////////////////////////

试试这个方法

import { configureStore } from '@reduxjs/toolkit'
import { combineReducers } from 'redux'
const reducer = combineReducers({
  // here we will be adding reducers
})
const store = configureStore({
  reducer,
})
export default store;

详情Here

实际上我得到了解决方案。

显示此错误是因为我已将 rootReducer 函数放在配置存储下面,这就是为什么 reducer id 没有获得 rootReducer 函数。

所以,当我向上移动它时,它工作正常。

当我在我的客户端 index.js 文件中配置我的 redux 存储时,我在 React 中也遇到了这个问题。这是我产生错误的代码:

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { configureStore } from "@reduxjs/toolkit";
import { Provider } from "react-redux";
import allReducers from "./redux/reducer";

const store = configureStore({ allReducers });

ReactDOM.render(
    <Provider store={store}>
        <App />
    </Provider>,
    document.getElementById("root")
);

快速修复: 正如错误提示的那样,"reducer" is a required argument... 所以,我只是像这样修改了我的存储常量:configureStore({reducer: allReducers})。我只是将“reducer”作为键,并将 allReducers 作为其值传递。然后我的问题就解决了。