Skip to content

Commit 8ac8439

Browse files
Riccardo Cipolleschifacebook-github-bot
Riccardo Cipolleschi
authored andcommitted
Prepare AppDelegate to enable Concurrent Root on iOS (#33671)
Summary: Pull Request resolved: #33671 With React 18, we now need to allow users on Fabric to opt-in for Concurrent Root. This commit adds a new method that can be customized in the AppDelegate to turn the feature on and off. The flag is passed as an initialProps to the rootView. ## Changelog: [iOS][Added] - Prepare a method in the AppDelegate to control the concurrentRoot. Reviewed By: cortinico, dmitryrykun Differential Revision: D35757833 fbshipit-source-id: 192cf74c796554cba39366aa90c53c191f960c20
1 parent 323db75 commit 8ac8439

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

packages/rn-tester/RNTester/AppDelegate.mm

+25-6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ @interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate>
7777
}
7878
@end
7979

80+
static NSString *const kRNConcurrentRoot = @"concurrentRoot";
81+
8082
@implementation AppDelegate
8183

8284
- (BOOL)application:(__unused UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
@@ -86,12 +88,7 @@ - (BOOL)application:(__unused UIApplication *)application didFinishLaunchingWith
8688
_bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
8789

8890
// Appetizer.io params check
89-
NSDictionary *initProps = @{};
90-
NSString *_routeUri = [[NSUserDefaults standardUserDefaults] stringForKey:@"route"];
91-
if (_routeUri) {
92-
initProps =
93-
@{@"exampleFromAppetizeParams" : [NSString stringWithFormat:@"rntester://example/%@Example", _routeUri]};
94-
}
91+
NSDictionary *initProps = [self prepareInitialProps];
9592

9693
#ifdef RN_FABRIC_ENABLED
9794
_contextContainer = std::make_shared<facebook::react::ContextContainer const>();
@@ -119,6 +116,28 @@ - (BOOL)application:(__unused UIApplication *)application didFinishLaunchingWith
119116
return YES;
120117
}
121118

119+
- (BOOL)concurrentRootEnabled
120+
{
121+
// Switch this bool to turn on and off the concurrent root
122+
return true;
123+
}
124+
125+
- (NSDictionary *)prepareInitialProps
126+
{
127+
NSMutableDictionary *initProps = [NSMutableDictionary new];
128+
129+
NSString *_routeUri = [[NSUserDefaults standardUserDefaults] stringForKey:@"route"];
130+
if (_routeUri) {
131+
initProps[@"exampleFromAppetizeParams"] = [NSString stringWithFormat:@"rntester://example/%@Example", _routeUri];
132+
}
133+
134+
#ifdef RCT_NEW_ARCH_ENABLED
135+
initProps[kRNConcurrentRoot] = @([self concurrentRootEnabled]);
136+
#endif
137+
138+
return initProps;
139+
}
140+
122141
- (NSURL *)sourceURLForBridge:(__unused RCTBridge *)bridge
123142
{
124143
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"packages/rn-tester/js/RNTesterApp.ios"];

template/ios/HelloWorld/AppDelegate.mm

+26-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#import <react/config/ReactNativeConfig.h>
1818

19+
static NSString *const kRNConcurrentRoot = @"concurrentRoot";
20+
1921
@interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate> {
2022
RCTTurboModuleManager *_turboModuleManager;
2123
RCTSurfacePresenterBridgeAdapter *_bridgeAdapter;
@@ -41,7 +43,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
4143
bridge.surfacePresenter = _bridgeAdapter.surfacePresenter;
4244
#endif
4345

44-
UIView *rootView = RCTAppSetupDefaultRootView(bridge, @"HelloWorld", nil);
46+
NSDictionary *initProps = [self prepareInitialProps];
47+
UIView *rootView = RCTAppSetupDefaultRootView(bridge, @"HelloWorld", initProps);
4548

4649
if (@available(iOS 13.0, *)) {
4750
rootView.backgroundColor = [UIColor systemBackgroundColor];
@@ -57,6 +60,28 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
5760
return YES;
5861
}
5962

63+
/// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
64+
///
65+
/// @see: https://reactjs.org/blog/2022/03/29/react-v18.html
66+
/// @note: This requires to be rendering on Fabric (i.e. on the New Architecture).
67+
/// @return: `true` if the `concurrentRoot` feture is enabled. Otherwise, it returns `false`.
68+
- (BOOL)concurrentRootEnabled
69+
{
70+
// Switch this bool to turn on and off the concurrent root
71+
return true;
72+
}
73+
74+
- (NSDictionary *)prepareInitialProps
75+
{
76+
NSMutableDictionary *initProps = [NSMutableDictionary new];
77+
78+
#ifdef RCT_NEW_ARCH_ENABLED
79+
initProps[kRNConcurrentRoot] = @([self concurrentRootEnabled]);
80+
#endif
81+
82+
return initProps;
83+
}
84+
6085
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
6186
{
6287
#if DEBUG

0 commit comments

Comments
 (0)