RNN on RN 0.71.1 is not linked/launched correctly in AppDelegate
What happened?
RNN 7.31.1 doesn't work with the RN app initialized using npx react-native init with the latest version of RN currently available (0.71.1). It shows Invariant Violation error on start:
Invariant Violation: "MyApp" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called., js engine: hermes
This is probably because AppDelegate.mm file template was changed from what we had in react-native-template-typescript. It's because AppDelegate now should inherit from RCTAppDelegate. Now there's no bridge initialisation there (it happens in the parent RCTAppDelegate class method). And the link stage doesn't add anything to the file except extraModulesForBridge:
#import "AppDelegate.h"
#import <ReactNativeNavigation/ReactNativeNavigation.h>
#import <React/RCTBundleURLProvider.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.moduleName = @"MyApp";
// 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];
}
- (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
return [ReactNativeNavigation extraModulesForBridge:bridge];
}
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}
/// 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` feature is enabled. Otherwise, it returns `false`.
- (BOOL)concurrentRootEnabled
{
return true;
}
@end
p.s. I'm experienced RNN user, the last time I used it was with RN 0.70.6 initialized with react-native-template-typescript tempalte and RNN 7.30.6 and everything worked correctly.
What was the expected behaviour?
No response
Was it tested on latest react-native-navigation?
- [X] I have tested this issue on the latest react-native-navigation release and it still reproduces.
Help us reproduce this issue!
I created the app using npx react-native init. RN version is 0.71.1. Then I did yarn add react-native-navigation, npx rnn-link and cd ios && bundle exec pod install, it was linked and installed correctly (there wasn't any errors).
Then updated root index.js file:
import { Navigation } from "react-native-navigation";
import App from "./App";
Navigation.registerComponent('MyApp.WelcomeScreen', () => App);
Navigation.events().registerAppLaunchedListener(() => {
Navigation.setRoot({
root: {
stack: {
children: [
{
component: {
name: 'MyApp.WelcomeScreen'
}
}
]
}
}
});
});
In what environment did this happen?
React Native Navigation version: 7.31.1 React Native version: 0.71.1 Has Fabric (React Native's new rendering system) enabled: no Node version: 16 Device model: Xcode Simulator Version 14.1 (986.5) iOS version: 16.1
I think it's not that hard to resolve this, just rewrote this method in app delegate https://github.com/facebook/react-native/blob/b314e6f147fdfcd71e7ba3b17338bb44356df98e/Libraries/AppDelegate/RCTAppDelegate.mm#L33 . But it worth to mention in docs, I can submit PR.
Resulting AppDelegate.mm is:
#import "AppDelegate.h"
#import <ReactNativeNavigation/ReactNativeNavigation.h>
#import <React/RCTAppSetupUtils.h>
#import <React/RCTBundleURLProvider.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.moduleName = @"MyApp";
// 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 = @{};
BOOL enableTM = NO;
#if RCT_NEW_ARCH_ENABLED
enableTM = self.turboModuleEnabled;
#endif
RCTAppSetupPrepareApp(application, enableTM);
if (!self.bridge) {
self.bridge = [self createBridgeWithDelegate:self launchOptions:launchOptions];
}
[ReactNativeNavigation bootstrapWithBridge:self.bridge];
#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);
self.bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:self.bridge
contextContainer:_contextContainer];
self.bridge.surfacePresenter = self.bridgeAdapter.surfacePresenter;
#endif
return YES;
}
- (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
return [ReactNativeNavigation extraModulesForBridge:bridge];
}
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}
/// 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` feature is enabled. Otherwise, it returns `false`.
- (BOOL)concurrentRootEnabled
{
return true;
}
@end
I have tried with this solution but it seem not working. It got crash right after start
@pid0r-3blan Thank you so much 🙏
After a week of trying I could finally make the React Native Navigation work inside RN 0.71.4
Important notes:
-
Remember to add:
#import <React/RCTAppSetupUtils.h> -
Remember to replace this line
return [super application:application didFinishLaunchingWithOptions:launchOptions];with this line:return YES;Possible Error: Invariant Violation: "MyAppName" has not been registered. -
Remember to delete these lines:
[ReactNativeNavigation bootstrapWithDelegate:self launchOptions:launchOptions]RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions][ReactNativeNavigation bootstrapWithBridge:bridge]Possible Error: Exception 'Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched()
Here's working AppDelegate.mm that I've generated for fresh app:
#import "AppDelegate.h"
#import <ReactNativeNavigation/ReactNativeNavigation.h>
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTAppSetupUtils.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>
@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
{
RCTAppSetupPrepareApp(application);
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
[ReactNativeNavigation bootstrapWithBridge:bridge];
#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;
#endif
return YES;
}
- (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge {
return [ReactNativeNavigation extraModulesForBridge:bridge];
}
- (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
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest version and report back. Thank you for your contributions.
The issue has been closed for inactivity.
Perhaps this issue should not remain closed as stale?
As far as I can tell, following the basic happy path instructions [1] on a fresh install of the latest version of react native (0.71.8 as of this writing) does not result in a working iOS app for the reasons @pid0r-3blan explained.
Specifically, it seems that rnn-link's assumptions about the code structure are no longer true.
[1] via https://wix.github.io/react-native-navigation/docs/installing#installing-with-npx-rnn-link
Fixed in #7733
I still can't get it to work. I tried all suggestions here. It either crashes, black screen, or I get an error that the app was not registered.
My app was getting stuck in splash screen, and it seems like my issue was that I was missing these lines:
if (!self.bridge) {
self.bridge = [self createBridgeWithDelegate:self launchOptions:launchOptions];
}
Also had to remove the moduleName since otherwise my navigation buttons would be unresponsive:
self.moduleName = @"AppName";
RCTBundleURLProvider
'React/RCTAppSetupUtils.h' file not found