1
I am following the Redux tutorials on the Redux site and I am having difficulty using combine reducers. If I run my code without combine reducers, it works fine. As soon as I put the single reducer into the combined reducers function. I get this: Error: "reducer" is a required argument, and must be a function or an object of functions that can be passed to combineReducers. What am I doing wrong?
Here is the store before using combine reducer
import {configureStore , combineReducers} from "@reduxjs/toolkit"
import { applyMiddleware } from "redux";
import thunk from "redux-thunk";
import { composeWithDevTools } from "redux-devtools-extension";
import { productReducer } from "./reducers/productReducer";
const rootReducer = combineReducers({
products:productReducer
});
let initialState = {};
const middleware = [thunk];
const store = configureStore(
rootReducer,
initialState,
composeWithDevTools(applyMiddleware(...middleware))
);
export default store;
As a first observation: you don't need to manually add the
thunkmiddleware, callcomposeWithDevTools, or callapplyMiddleware- theconfigureStoreAPI already does all of those for you!More specifically in this case: we normally call the top-level reducer function the "root reducer" to categorize it.
However:
configureStoreexpects that you pass that as a field namedreducer, and instead you're passing a field namedrootReducer. So, it's correctly pointing out that there is noreducerargument being provided.Change it to
reducer: rootReducerand it will work.For that matter, it's
preloadedState, and notinitialState.However,
configureStorewill also automatically callcombineReducersfor you, and you don't need to provide an empty object. So this whole thing could really be as short as: