If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don’t want to set a value for this reducer, you can use null instead of undefined.
store.js
import { configureStore } from "@reduxjs/toolkit";
import { apiSlice } from "../slices/apiSlice";
import cartSliceReducer from "../slices/cartSlice.js";
const store = configureStore({
reducer: {
[apiSlice.reducerPath]: apiSlice.reducer,
cart:cartSliceReducer,
},
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat
(apiSlice.middleware),
devTools: true
})
export default store;
at store.js:5:15
cartSlice.js
import { createSlice } from "@reduxjs/toolkit";
const initialstate = localStorage.getItem('cart') ? JSON.parse(localStorage.getItem('cart')) :
{ cartItems: [] };
const cartSlice = createSlice({
name: 'cart',
initialstate,
reducers: {
}
})
export default cartSlice.reducer;
New contributor
Siddik Mulla is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.