单个应用程序中的多个 RCTRootView [英] Multiple RCTRootView in a single App

查看:30
本文介绍了单个应用程序中的多个 RCTRootView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a react native app but i require the use of objective C to build my custom UIView. But issue is that this UIView need to display react content. e.g. a react text input and button. I am thinking about adding a RCTRootView (which will be having these react components) to the Custom UIView and thus solve the issue. But I am currently using one RCTRootView. How do i create another one and use it in my code.

我确实明白我们可以从服务器添加不同的包 how-to-rename-react-native-entry-file-index-ios-js 但是当我在本地运行时如何使用它.

edit: I did understand that we can add different bundle with from server how-to-rename-react-native-entry-file-index-ios-js but how to use that when i am running locally.

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

推荐答案

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

Create another RCTRootView, as you would any other UIView.

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

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

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

You can specify the same bundle (jsCodeLocation) for all RCTRootViews, or different bundles for each RCTRootView. In either case its a best practice to have different component names (moduleName):

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

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

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

If you would like to maintain multiple bundles you will need to compile each with the command:

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 然后可以添加到您的项目中并正常引用.

main1.jsbundle and main2.jsbundle can then be added to your project and referenced normally.

这篇关于单个应用程序中的多个 RCTRootView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆