So I’m getting Uncaught Error: when using a middleware builder function, an array of middleware must be returned
This is my code
import { configureStore, compose, combineReducers, applyMiddleware } from "@reduxjs/toolkit";
import thunk from "redux-thunk";
const rootReducer = combineReducers({});
const middleware = applyMiddleware(thunk);
const composeWithDevTools = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const composedMiddleWare = composeWithDevTools(middleware)
const store = configureStore({
reducer: rootReducer,
middleware: composedMiddleWare,
devTools: window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
})
export default store;
I have no idea what’s wrong and searching doesn’t seem to be returning any useful result
2
Answers
from this line
the array is empty it cannot be empty us have to put in a slice. for example if you want to use a userSlice, the you have to add it there like this:
notice i add a userSlice to the array
The error you’re encountering is caused by the incorrect usage of the
applyMiddleware
function from Redux. In Redux Toolkit, theconfigureStore
function already applies the middleware internally, so you don’t need to useapplyMiddleware
separately.Here’s the corrected code: