React Native 混合开发多入口加载方式详解
2019-09-23 14:18
1151 查看
在已有app混合开发时,可能会有多个rn界面入口的需求,这个时候我们可以使用RCTRootView中的moduleName或initialProperties来实现加载包中的不同页面。
目前使用RCTRootView有两种方式:
- 使用initialProperties传入props属性,在React中读取属性,通过逻辑来渲染不同的Component
- 配置moduleName,然后AppRegistry.registerComponent注册同名的页面入口
这里贴出使用0.60.5版本中ios项目的代码片段:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"AwesomeProject" initialProperties: @{ @"screenProps" : @{ @"initialRouteName" : @"Home", }, }]; 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; }
initialProperties
这种方式简单使用可以通过state判断切换界面,不过项目使用中还是需要react-navigation这样的导航组件搭配使用,下面贴出的代码就是结合路由的实现方案。
screenProps是react-navigation中专门用于传递给React组件数据的属性,createAppContainer创建的组件接受该参数screenProps,并传给访问的路由页面。
class App extends React.Component { render() { const { screenProps } = this.props; const stack = createStackNavigator({ Home: { screen: HomeScreen, }, Chat: { screen: ChatScreen, }, }, { initialRouteName: screenProps.initialRouteName || 'Home', }); const AppContainer = createAppContainer(stack); return ( <AppContainer screenProps /> ); } }
moduleName
我们按照下面代码注册多个页面入口之后,就可以在原生代码中指定moduleName等于AwesomeProject或者AwesomeProject2来加载不同页面。
AppRegistry.registerComponent("AwesomeProject", () => App); AppRegistry.registerComponent("AwesomeProject2", () => App2);
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- 【React Native开发】React Native 进阶之原生混合与数据通信开发详解-适配iOS开发(61)
- [React Native混合开发]React Native for iOS之布局实战(二)
- Android native和h5混合开发几种常见的hybrid通信方式
- 混合开发的大趋势之一React Native State (状态),Style(样式)
- React-Native 桥接iOS原生开发详解
- 《React Native 精解与实战》书籍连载「Android 平台与 React Native 混合开发」
- [React Native混合开发]React Native for iOS之UI组件
- 【React Native开发】React Native 控件之Modal详解-Android/iOS双平台通用(56)
- 混合开发的大趋势之一React Native State (状态),Style(样式)
- 混合开发的大趋势之一React Native之页面跳转
- [React Native混合开发]React Native中的Flexbox布局
- [React Native混合开发]React Native for iOS之代码结构
- React-native、原生混合开发AndroidStudio打包流程(Windows 10)
- 【React Native开发】 - Image的加载技巧与使用
- 混合开发的大趋势之一React Native之简单的登录界面
- React Native Image多种加载图片方式
- 【React Native】刷新组件RENDER(重新渲染)的三种方式详解
- [React Native混合开发]React Native for iOS之动手写组件
- Mac android原生工程中潜入react-native混合开发项目搭建
- 混合开发的大趋势之一React Native之页面跳转