React Native 0.71.2升级后出现RCTAppSetupDefaultRootView调用匹配错误
React Native 0.71.2升级后报错:'No matching function for call to 'RCTAppSetupDefaultRootView''
'No matching function for call to 'RCTAppSetupDefaultRootView'
我的AppDelegate.mm代码
#import "AppDelegate.h" #import <React/RCTBridge.h> #import <React/RCTBundleURLProvider.h> #import <React/RCTRootView.h> #import <React/RCTAppSetupUtils.h> #import <Firebase.h> #if RCT_NEW_ARCH_ENABLED #import <React/CoreModulesPlugins.h> #import <React/RCTCxxBridgeDelegate.h> #import <React/RCTFabricSurfaceHostingProxyRootView.h> #import <React/RCTSurfacePresenter.h> #import <React/RCTSurfacePresenterBridgeAdapter.h> #import <ReactCommon/RCTTurboModuleManager.h> #import <react/config/ReactNativeConfig.h> static NSString *const kRNConcurrentRoot = @"concurrentRoot"; @interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate> { RCTTurboModuleManager *_turboModuleManager; RCTSurfacePresenterBridgeAdapter *_bridgeAdapter; std::shared_ptr<const facebook::react::ReactNativeConfig> _reactNativeConfig; facebook::react::ContextContainer::Shared _contextContainer; } @end #endif @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [FIRApp configure]; RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; #if RCT_NEW_ARCH_ENABLED _contextContainer = std::make_shared<facebook::react::ContextContainer const>(); _reactNativeConfig = std::make_shared<facebook::react::EmptyReactNativeConfig const>(); _contextContainer->insert("ReactNativeConfig", _reactNativeConfig); _bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:bridge contextContainer:_contextContainer]; bridge.surfacePresenter = _bridgeAdapter.surfacePresenter; [FIRApp configure]; #endif NSDictionary *initProps = [self prepareInitialProps]; UIView *rootView = RCTAppSetupDefaultRootView(bridge, @"XplosionTMBD", initProps); if (@available(iOS 13.0, *)) { rootView.backgroundColor = [UIColor systemBackgroundColor]; } else { rootView.backgroundColor = [UIColor whiteColor]; } self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UIViewController *rootViewController = [UIViewController new]; rootViewController.view = rootView; self.window.rootViewController = rootViewController; [self.window makeKeyAndVisible]; return YES; } /// This method controls whether the `concurrentRoot`feature of React18 is turned on or off. /// /// @see: https://reactjs.org/blog/2022/03/29/react-v18.html /// @note: This requires to be rendering on Fabric (i.e. on the New Architecture). /// @return: `true` if the `concurrentRoot` feture is enabled. Otherwise, it returns `false`. - (BOOL)concurrentRootEnabled { // Switch this bool to turn on and off the concurrent root return true; } - (NSDictionary *)prepareInitialProps { NSMutableDictionary *initProps = [NSMutableDictionary new]; #ifdef RCT_NEW_ARCH_ENABLED initProps[kRNConcurrentRoot] = @([self concurrentRootEnabled]); #endif return initProps; } - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge { #if DEBUG return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"]; #else return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"]; #endif } #if RCT_NEW_ARCH_ENABLED #pragma mark - RCTCxxBridgeDelegate - (std::unique_ptr<facebook::react::JSExecutorFactory>)jsExecutorFactoryForBridge:(RCTBridge *)bridge { _turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge delegate:self jsInvoker:bridge.jsCallInvoker]; return RCTAppSetupDefaultJsExecutorFactory(bridge, _turboModuleManager); } #pragma mark RCTTurboModuleManagerDelegate - (Class)getModuleClassFromName:(const char *)name { return RCTCoreModulesClassProvider(name); } - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker { return nullptr; } - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name initParams: (const facebook::react::ObjCTurboModule::InitParams &)params { return nullptr; } - (id<RCTTurboModule>)getModuleInstanceFromClass:(Class)moduleClass { return RCTAppSetupDefaultModuleFromClass(moduleClass); } #endif @end
react-native info 输出
System: OS: macOS 12.5.1 CPU: (8) arm64 Apple M1 Memory: 201.66 MB / 16.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 18.12.0 - /usr/local/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 8.19.2 - /usr/local/bin/npm Watchman: 2023.02.06.00 - /opt/homebrew/bin/watchman Managers: CocoaPods: 1.11.3 - /opt/homebrew/bin/pod SDKs: iOS SDK: Platforms: DriverKit 22.2, iOS 16.2, macOS 13.1, tvOS 16.1, watchOS 9.1 Android SDK: Not Found IDEs: Android Studio: Not Found Xcode: 14.2/14C18 - /usr/bin/xcodebuild Languages: Java: Not Found npmPackages: @react-native-community/cli: ^10.1.3 => 10.1.3 react: ^18.2.0 => 18.2.0 react-native: 0.71.2 => 0.71.2 react-native-macos: Not Found npmGlobalPackages: *react-native*: Not Found
已尝试降级重升级React Native、卸载重装Pods、重装NPM包、遵循官方升级文档,但构建仍失败,期望实现构建成功。
问题原因与解决办法
报错根源
React Native 0.71.x版本调整了RCTAppSetupDefaultRootView的函数签名,同时新架构(Fabric)和旧架构的RootView初始化逻辑完全不同,你的代码没有针对两种架构做区分处理,直接调用旧的函数形式就会触发匹配失败的错误。
修复代码
把AppDelegate.mm中创建rootView的代码块替换为以下内容:
NSDictionary *initProps = [self prepareInitialProps]; UIView *rootView; #if RCT_NEW_ARCH_ENABLED // 新架构下使用专属的RootView类 rootView = [[RCTFabricSurfaceHostingProxyRootView alloc] initWithBridge:bridge moduleName:@"XplosionTMBD" initialProperties:initProps]; #else // 旧架构下保留原函数调用 rootView = RCTAppSetupDefaultRootView(bridge, @"XplosionTMBD", initProps); #endif
额外检查项
- 确认ios/Podfile中的
$RCT_NEW_ARCH_ENABLED值(true为新架构,false为旧架构)和你实际使用的架构一致,修改后重新执行pod install。 - 清理Xcode缓存:按
Cmd+Shift+K清理构建产物,手动删除~/Library/Developer/Xcode/DerivedData目录下的缓存文件。 - 检查Podfile.lock中的React-Core版本是否为0.71.2,确保Pods依赖已正确更新。
内容的提问来源于stack exchange,提问作者khalil yahi
相关产品推荐
相关产品推荐

