| #
0f5f192f |
| 01-Dec-2021 |
Kudo Chien <[email protected]> |
[core] Introduce iOS ReactDelegate for React related instances creation (#15138)
# Why
original modularized AppDelegateWrapper isn't ideal for expo-updates. we had some bad side effect, e.g. doub
[core] Introduce iOS ReactDelegate for React related instances creation (#15138)
# Why
original modularized AppDelegateWrapper isn't ideal for expo-updates. we had some bad side effect, e.g. double bridge creation on startup.
close ENG-2299
# How
## For module authors
introduce `EXReactDelegate` dedicated for react related instances creation. so far we covered:
- RCTBridge
- RCTRootView
- RootViewController (for expo-screen-orientation)
if a module wants to handle these creation, there's 2 steps:
1. add `reactDelegateHandlers` in `expo-module.config.json`, e.g.
```
"ios": {
"reactDelegateHandlers": ["ScreenOrientationReactDelegateHandler"]
}
```
2. create `EXReactDelegateHandler` derived swift class, e.g.
```swift
// ScreenOrientationReactDelegateHandler.swift
public class ScreenOrientationReactDelegateHandler: ExpoReactDelegateHandler {
public override func createRootViewController(reactDelegate: ExpoReactDelegate) -> UIViewController? {
return EXScreenOrientationViewController()
}
}
```
since the order of whom to create the instance did matter, we also introduce `ModulePriorities.swift` similar to what we did on android.
## For app developers
the change to template is minimal, so codemod by `npx install-expo-modules` is also feasible.
```diff
- RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
- RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"main" initialProperties:nil];
+ RCTBridge *bridge = [self.reactDelegate createBridgeWithDelegate:self launchOptions:launchOptions];
+ RCTRootView *rootView = [self.reactDelegate createRootViewWithBridge:bridge moduleName:@"main" initialProperties:nil];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
- UIViewController *rootViewController = [UIViewController new];
+ UIViewController *rootViewController = [self.reactDelegate createRootViewController];
```
show more ...
|