skip to Main Content

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


  1. first of all: I think there is no need to combine reducers as configure store does this under the hood (RtK Docs)

    configureStore(): wraps createStore to provide simplified configuration options and good defaults. It can automatically combine your slice reducers, adds whatever Redux middleware you supply, includes redux-thunk by default, and enables use of the Redux DevTools Extension.

    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)

    Login or Signup to reply.
  2. You are exporting as a named export, but importing a default export.

    You need to do

    import { rootReducer } from "./reducers/rootReducer.js";
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search