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

React Native应用报错:预期根reducer为函数,实际接收到undefined

问题原因
  • 导入的reducer变量名不匹配:你在reducer入口文件默认导出的变量名为Reducers,但创建store时使用的变量名为rootReducers,如果导入时未做重命名,拿到的变量实际为undefined,直接触发报错。
  • combineReducers调用不符合规范:该方法要求必须传入包含子reducer的对象作为入参,你当前是空调用,即便变量名修正,生成的reducer也不符合Redux要求。
修复方案
  1. 修正reducer入口文件(你的index.js)代码,给combineReducers传入合法参数,同时统一导出变量名:
import { combineReducers } from "redux";

// 临时占位reducer,后续替换为你的业务reducer即可
const baseReducer = (state = {}, action) => {
  switch (action.type) {
    default:
      return state;
  }
};

const rootReducers = combineReducers({
  // 后续所有业务reducer都在这个对象中注册
  base: baseReducer
});

export default rootReducers;
  1. 修正App.js中的导入逻辑,确保导入的reducer变量名与导出一致:
// 路径根据你的实际项目目录调整即可
import rootReducers from './reducers'; 

const store = createStore(rootReducers, composeWithDevTools(applyMiddleware(thunk)));

修改完成后重新执行expo start即可解决该报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 06:36:02