React Native 中的 RCTBridge
RCTBridge in React Native
如果我的移动应用有多个 RCTRootView 使用。我应该重复使用相同的 RCTBridge 还是为每个创建一个。假设两个 RCTRootView 可以在相同的 VC 或不同的 VC 上。谢谢。
建议整个移动应用只有一个网桥。我将证明 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];
如果我的移动应用有多个 RCTRootView 使用。我应该重复使用相同的 RCTBridge 还是为每个创建一个。假设两个 RCTRootView 可以在相同的 VC 或不同的 VC 上。谢谢。
建议整个移动应用只有一个网桥。我将证明 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];