React Native之AppRegistry模块

时间:2022-04-26
本文章向大家介绍React Native之AppRegistry模块,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

我们在写react native的js的时候,在最后总会加上一段代码:

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

代码的意思:定义了一个名为ReactDemo的新的组件(Component),并且使用了名为AppRegistry的内置模块进行了“注册”操作。在编写React Native应用时,肯定会写出很多新的组件。而一个App的最终界面,其实也就是各式各样的组件的组合。这和android和ios的思路不谋而合,其实React Native的组件也很丰富。看官方提供的常用组件:

AppRegistry模块则是用来告知React Native哪一个组件被注册为整个应用的根容器。

使用AppRegistry.registerComponent进行注册自己,然后原生系统就可以进行加载运行bundle文件包,最后就会可以调用AppRegistry.runApplication进行运行起来应用。当一个视图被摧毁的时候,为了结束应用需要调用AppRegistry.unmountApplictionComponentAtRootTag方法。

AppRegistry常用方法

.registerConfig(config:Array<AppConfig>)  static 静态方法, 进行注册配置信息

.registerComponent(appKey:string,getComponentFunc:ComponentProvider)  static静态方法,进行注册组件

.registerRunnable(appKey:string,func:Function)  static静态方法 ,进行注册线程

.registerAppKeys()  static静态方法,进行获取所有组件的keys值

.runApplication(appKey:string,appParameters:any)  static静态方法, 进行运行应用

.unmountApplicationComponentAtRootTag()  static静态方法,结束应用

AppRegistry是React中最基本的模块,以后会慢慢讲解。