React Native从0.70.5升级至0.72.6后出现JS异常求助
问题:React Native升级后出现"Value is an object, expected an Object"错误
我用Ignite模板开发React Native应用,因升级Xcode 15,不得不将react-native从0.70.5升级到0.72.6。编译成功后遇到如下错误:
Unhandled JS Exception: Value is an object, expected an Object
Error: Value is an object, expected an Object
我试过npx react-native upgrade,也借助React Native升级助手修改配置完成了Xcode编译,但问题仍存在。该错误无堆栈跟踪,Xcode日志及模拟器均显示对应错误界面;点击模拟器错误弹窗的Reload后,调试器会停在指定位置。我是RN和Xcode新手,已耗时2天,求调试思路。
附上AppDelegate相关代码:
AppDelegate.m
#import "AppDelegate.h" #import "RNBootSplash.h" #import <React/RCTBundleURLProvider.h> @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.moduleName = @"App"; // You can add your custom initial props in the dictionary below. // They will be passed down to the ViewController used by React Native. self.initialProps = @{}; return [super application:application didFinishLaunchingWithOptions:launchOptions]; } - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge { #if DEBUG return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"]; #else return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"]; #endif } @end
AppDelegate.h
#import <RCTAppDelegate.h> #import <UIKit/UIKit.h> @interface AppDelegate : RCTAppDelegate @end
调试思路
- 强制输出完整错误堆栈:在项目入口文件(如
index.js)顶部添加Error.stackTraceLimit = Infinity;,强制生成完整堆栈信息,定位错误来源的具体文件和行号。 - 排查第三方依赖兼容性:Ignite模板自带较多第三方库,逐个检查依赖版本是否适配react-native 0.72.6。可先临时注释非核心依赖(比如RNBootSplash),观察错误是否消失,逐步定位问题库。
- 彻底清理缓存:执行以下命令清除各类缓存,避免旧代码残留:
rm -rf node_modules && npm install npx react-native clean rm -rf ~/Library/Developer/Xcode/DerivedData - 启用JS远程调试断点:打开模拟器菜单(Cmd+D)选择
Debug JS Remotely,在Chrome调试器中设置全局异常断点,Reload后跟踪错误触发的具体位置。 - 检查InitialProps配置:尝试注释掉
self.initialProps = @{};代码,观察错误是否变化,确认初始属性传递是否符合RN 0.72的要求。 - 对比官方纯净模板:新建一个react-native 0.72.6纯净项目,对比其AppDelegate代码及配置文件,检查自身项目是否存在遗漏或错误的配置项。
内容的提问来源于stack exchange,提问作者amit
相关产品推荐
相关产品推荐

