AppRegistry
AppRegistry
是所有 React Native 应用的 JS 入口。应用的根组件应当通过AppRegistry.registerComponent
方法注册自己,然后原生系统才可以加载应用的代码包并且在启动完成之后通过调用AppRegistry.runApplication
来真正运行应用。
import {Text, AppRegistry} from 'react-native';
const App = () => (
<View>
<Text>App1</Text>
</View>
);
AppRegistry.registerComponent('Appname', () => App);
要“结束”一个应用并销毁视图的话,请调用AppRegistry.unmountApplicationComponentAtRootTag
方法,参数为在runApplication
中使用的标签名。它们必须严格匹配。
AppRegistry
应当在require
序列中尽可能早的被 require 到,以确保 JS 运行环境在其它模块之前被准备好。
文档
方法
getAppKeys()
static getAppKeys(): string[];
返回一个 AppKeys 数组。
getRegistry()
static getRegistry(): {sections: string[]; runnables: Runnable[]};
返回一个Registry对象。
getRunnable()
static getRunnable(appKey: string): : Runnable | undefined;
返回一个Runnable对象。
参数:
名称 | 类型 |
---|---|
appKey Required | string |
getSectionKeys()
static getSectionKeys(): string[];
Returns an array of strings.
getSections()
static getSections(): Record<string, Runnable>;
Returns a Runnables object.
registerCancellableHeadlessTask()
static registerCancellableHeadlessTask(
taskKey: string,
taskProvider: TaskProvider,
taskCancelProvider: TaskCancelProvider,
);
Register a headless task which can be cancelled. A headless task is a bit of code that runs without a UI.
Parameters:
Name | Type | Description |
---|---|---|
taskKey Required | string | The native id for this task instance that was used when startHeadlessTask was called. |
taskProvider Required | TaskProvider | A promise returning function that takes some data passed from the native side as the only argument. When the promise is resolved or rejected the native side is notified of this event and it may decide to destroy the JS context. |
taskCancelProvider Required | TaskCancelProvider | a void returning function that takes no arguments; when a cancellation is requested, the function being executed by taskProvider should wrap up and return ASAP. |
registerComponent()
static registerComponent(
appKey: string,
getComponentFunc: ComponentProvider,
section?: boolean,
): string;
参数:
名称 | 类型 |
---|---|
appKey 必填 | string |
componentProvider 必填 | ComponentProvider |
section | boolean |
registerConfig()
static registerConfig(config: AppConfig[]);
参数:
名称 | 类型 |
---|---|
config Required | AppConfig[] |
registerHeadlessTask()
static registerHeadlessTask(
taskKey: string,
taskProvider: TaskProvider,
);
Register a headless task. A headless task is a bit of code that runs without a UI.
This is a way to run tasks in JavaScript while your app is in the background. It can be used, for example, to sync fresh data, handle push notifications, or play music.
Parameters:
Name | Type | Description |
---|---|---|
taskKey Required | string | The native id for this task instance that was used when startHeadlessTask was called. |
taskProvider Required | TaskProvider | A promise returning function that takes some data passed from the native side as the only argument. When the promise is resolved or rejected the native side is notified of this event and it may decide to destroy the JS context. |
registerRunnable()
static registerRunnable(appKey: string, func: Runnable): string;
Parameters:
Name | Type |
---|---|
appKey Required | string |
run Required | function |
registerSection()
static registerSection(
appKey: string,
component: ComponentProvider,
);
Parameters:
Name | Type |
---|---|
appKey Required | string |
component Required | ComponentProvider |
runApplication()
static runApplication(appKey: string, appParameters: any): void;
加载 JavaScript 包并运行应用程序。
参数:
Name | Type |
---|---|
appKey Required | string |
appParameters Required | any |
setComponentProviderInstrumentationHook()
static setComponentProviderInstrumentationHook(
hook: ComponentProviderInstrumentationHook,
);
参数:
Name | Type |
---|---|
hook Required | function |
A valid hook
function accepts the following as arguments:
Name | Type |
---|---|
component Required | ComponentProvider |
scopedPerformanceLogger Required | IPerformanceLogger |
The function must also return a React Component.
setWrapperComponentProvider()
static setWrapperComponentProvider(
provider: WrapperComponentProvider,
);
Parameters:
Name | Type |
---|---|
provider Required | ComponentProvider |
startHeadlessTask()
static startHeadlessTask(
taskId: number,
taskKey: string,
data: any,
);
Only called from native code. Starts a headless task.
Parameters:
Name | Type | Description |
---|---|---|
taskId Required | number | The native id for this task instance to keep track of its execution. |
taskKey Required | string | The key for the task to start. |
data Required | any | The data to pass to the task. |
unmountApplicationComponentAtRootTag()
static unmountApplicationComponentAtRootTag(rootTag: number);
Stops an application when a view should be destroyed.
Parameters:
Name | Type |
---|---|
rootTag Required | number |
Type Definitions
AppConfig
Application configuration for the registerConfig
method.
Type |
---|
object |
Properties:
Name | Type |
---|---|
appKey Required | string |
component | ComponentProvider |
run | function |
section | boolean |
Note: Every config is expected to set either
component
orrun
function.
Registry
Type |
---|
object |
Properties:
Name | Type |
---|---|
runnables | array of Runnables |
sections | array of strings |
Runnable
Type |
---|
object |
Properties:
Name | Type |
---|---|
component | ComponentProvider |
run | function |
Runnables
An object with key of appKey
and value of type of Runnable
.
Type |
---|
object |
Task
A Task
is a function that accepts any data as argument and returns a Promise that resolves to undefined
.
Type |
---|
function |
TaskCanceller
A TaskCanceller
is a function that accepts no argument and returns void.
Type |
---|
function |
TaskCancelProvider
A valid TaskCancelProvider
is a function that returns a TaskCanceller
.
Type |
---|
function |
TaskProvider
A valid TaskProvider
is a function that returns a Task
.
Type |
---|
function |