// my store.js
import {createStore, combineReducers, applyMiddleware} from "redux";
import thunk from "redux-thunk";
import {composeWithDevTools} from "redux-devtools-extension";
import {
//productsDetailReducer,
productsReducer,
} from "./reducers/productReducer";
import {userReducer} from "./reducers/useReducer";
const reducer = combineReducers({
products: productsReducer,
//productDetails: productsDetailReducer,
user: userReducer,
});
const initialState = null;
const middleware = [thunk];
const store = createStore(
reducer,
initialState,
composeWithDevTools(applyMiddleware(...middleware))
);
export default store;
//ProductReducer.js
import {
ALL_PRODUCT_FAIL,
ALL_PRODUCT_REQUEST,
ALL_PRODUCT_SUCCESS,
CLEAR_ERRORS,
} from "../constants/productConstants";
export const productsReducer = (state = {products: []}, action) => {
switch (action.type) {
case ALL_PRODUCT_REQUEST:
return {
loading: true,
product: [],
};
case ALL_PRODUCT_SUCCESS:
return {
loading: false,
product: action.payload.products,
productsCount: action.payload.productsCount,
};
case ALL_PRODUCT_FAIL:
return {
loading: false,
product: action.payload,
};
case CLEAR_ERRORS:
return {
...state,
error: null,
};
default:
return state;
}
};
//index.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
//import {BrowserRouter} from "react-router-dom";
import {Provider} from "react-redux";
import store from "./store";
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
//Error
Uncaught Error: The slice reducer for key "user" returned undefined during initialization. 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.
Just got the same error. Took me an hour to realize I haven't set the default case in my reducer. You have not provided userReducer code but I think the default case is the issue. Try returning the state in default case. It'll work