RactBridge在React Native中

时间:2017-10-26 00:57:55

标签: react-native react-native-android react-native-ios

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

1 个答案:

答案 0 :(得分:3)

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

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

<强> AppDelegate.h

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

<强> 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;
}

您可以像这样访问任何地方的桥梁。

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

   [[RCTRootView alloc] initWithBridge:appdelegate.reactBridge moduleName:@"ListViewNetworking" initialProperties:nil];