单个应用程序中的多个 RCTRootView

2024-02-12

我正在创建一个 React Native 应用程序,但我需要使用 Objective C 来构建我的自定义 UIView。但问题是这个 UIView 需要显示反应内容。例如反应文本输入和按钮。我正在考虑向自定义 UIView 添加 RCTRootView (它将包含这些反应组件),从而解决问题。但我目前正在使用一个 RCTRootView。我如何创建另一个并在我的代码中使用它。

编辑: 我确实明白我们可以从服务器添加不同的捆绑包如何重命名-react-native-entry-file-index-ios-js https://stackoverflow.com/questions/30694367/how-to-rename-react-native-entry-file-index-ios-js但当我在本地运行时如何使用它。

jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];


创建另一个 RCTRootView,就像创建任何其他 UIView 一样。

RCTRootView *someRootView = [[RCTRootView alloc] initWithBundleURL:someJsCodeLocation 
                                                        moduleName:@"SomeRootComponent" 
                                                     launchOptions:nil];

RCTRootView *anotherRootView = [[RCTRootView alloc] initWithBundleURL:anotherJsCodeLocation 
                                                           moduleName:@"AnotherRootComponent" 
                                                        launchOptions:nil];

您可以指定相同的包(jsCodeLocation) 对于所有 RCTRootView,或每个 RCTRootView 的不同捆绑包。在任何一种情况下,最好的做法是使用不同的组件名称(moduleName):

AppRegistry.registerComponent('SomeRootComponent', () => SomeRootComponent);

AppRegistry.registerComponent('AnotherRootComponent', () => AnotherRootComponent);

如果您想维护多个捆绑包,则需要使用以下命令编译每个捆绑包:

curl 'http://localhost:8082/index1.ios.bundle?dev=false&minify=true' -o iOS/main1.jsbundle

curl 'http://localhost:8082/index2.ios.bundle?dev=false&minify=true' -o iOS/main2.jsbundle

然后可以将 main1.jsbundle 和 main2.jsbundle 添加到您的项目中并正常引用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

单个应用程序中的多个 RCTRootView 的相关文章

随机推荐