React Native 中的 RCTBridge

Ken*_*Lim 2 react-native react-native-android react-native-ios

如果我的移动应用程序有多个 RCTRootView 用法。我应该重用相同的 RCTBridge 还是为每个创建一个。假设两个 RCTRootView 可以在同一个 VC 或不同的 VC 上。谢谢你。

Kum*_*ddy 5

建议整个移动应用程序只有一个桥接器。我将证明 iOS 的代码。

我将在主应用程序中创建桥,并将在任何地方使用。

AppDelegate.h

    @interface AppDelegate : UIResponder<UIApplicationDelegate,RCTBridgeDelegate>
         @property (nonatomic, strong) UIWindow *window;
         @property (strong) RCTBridge *reactBridge;
    @end
Run Code Online (Sandbox Code Playgroud)

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

  self.reactBridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];


  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:self.reactBridge moduleName:@"ListViewNetworking" initialProperties:nil];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  return YES;
}
Run Code Online (Sandbox Code Playgroud)

您可以像这样随时随地访问这座桥。

  AppDelegate *appdelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

   [[RCTRootView alloc] initWithBridge:appdelegate.reactBridge moduleName:@"ListViewNetworking" initialProperties:nil];
Run Code Online (Sandbox Code Playgroud)