When using Expo to build the app, I get this error:
Error: "reducer" is a required argument, and must be a function or an object of functions that can be passed to combineReducers, js engine: hermes
I am using combineReducers() to put all of them together and then creating the store with configureStore() since createStore() is deprecated.
this is my reducer bundler:
import { combineReducers } from "@reduxjs/toolkit";
import { commentReducer } from "./commentReducer.js";
import { lessonReducer } from "./lessonReducer.js";
import { studentReducer } from "./studentReducer.js";
import { subjectReducer } from "./subjectReducer.js";
import { tokenReducer } from "./tokenReducer.js";
import { userReducer } from "./userReducer.js";
export const rootReducer = combineReducers({
user: userReducer,
token: tokenReducer,
subject: subjectReducer,
student: studentReducer,
lesson: lessonReducer,
comment: commentReducer,
});
and this is where I create the store:
import { configureStore } from "@reduxjs/toolkit";
import rootReducer from "./reducers/rootReducer.js";
const store = configureStore({
reducer: rootReducer,
});
export default store;
This is one of the reducers, they are all exactly the same because I just created them by copying to later on fill:
import * as types from "../actions/types";
const initialState = {
token: "",
loading: false,
error: null,
};
export const tokenReducer = (state = initialState, action) => {
switch (action.type) {
case types.LOGIN_REQUEST:
return { ...state, loading: true, error: null };
case types.LOGIN_SUCCESS:
return { ...state, loading: false, token: action.payload, error: null };
case types.LOGIN_FAILURE:
return { ...state, loading: false, error: action.payload, token: "" };
default:
return state;
}
};
I tried commenting every single file where there is a reducer and still get the same error, any ideas on how I can fix it?
2
Answers
first of all: I think there is no need to combine reducers as configure store does this under the hood (RtK Docs)
Second thing: I think if you are going to use RTK, you should stick to the way of creating reducers mentioned in the Docs (ie: use createReducer function)
You are exporting as a named export, but importing a default export.
You need to do
That said, the style of Redux you are writing here is 4+ years outdated at this point – modern Redux doesn’t use
combineReducers
,switch..case
statements,ACTION_TYPES
or hand-written immutable reducer update logic.Please read the official Redux Tutorial to learn modern Redux.