You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Expo构建应用时Redux reducer参数错误排查求助

解决Expo中Redux的"reducer is a required argument"错误

问题描述

遇到错误:

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

使用combineReducers整合reducer,再通过configureStore创建store(替代已弃用的createStore),相关代码如下:

rootReducer文件

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,
});

创建store的代码

import { configureStore } from "@reduxjs/toolkit";
import rootReducer from "./reducers/rootReducer.js";

const store = configureStore({
  reducer: rootReducer,
});

export default store;

单个reducer示例(所有reducer结构一致)

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;
  }
};

尝试注释所有reducer文件后仍报错,求解决方法。

解决步骤

  • 检查导出导入匹配性:确保每个reducer文件用export const xxxReducer命名导出,rootReducer里用import { xxxReducer }对应导入。若某个reducer用了export default,导入时要改成import xxxReducer from './xxx.js',否则会导入undefined导致combineReducers失效。
  • 校验文件路径:确认所有reducer的导入路径无拼写错误,注意文件名大小写(部分系统区分大小写),避免路径错误导致导入失败。
  • 排查循环依赖:检查是否存在循环导入(比如reducer导入action,action又导入store/rootReducer),循环依赖会导致模块未初始化完成,出现undefined。
  • 清除缓存重启:执行expo start -c清除Expo缓存,再重新启动项目,避免旧缓存代码残留引发问题。
  • 逐步定位问题reducer:临时简化store配置,比如直接在configureStore里只传入一个确定正常的reducer(如reducer: { token: tokenReducer }),若不再报错,再逐个添加其他reducer,定位出异常文件。

内容的提问来源于stack exchange,提问作者Zerkley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 17:35:17