跳至主内容
版本:0.77

AppRegistry

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

AppRegistry 是所有 React Native 应用的 JavaScript 运行入口点。应用根组件应通过 AppRegistry.registerComponent 注册自身,随后原生系统将加载应用包,并在准备就绪时通过调用 AppRegistry.runApplication 真正运行应用。

tsx
import {Text, AppRegistry} from 'react-native';

const App = () => (
<View>
<Text>App1</Text>
</View>
);

AppRegistry.registerComponent('Appname', () => App);

当视图需要销毁时,要"停止"应用,请调用 AppRegistry.unmountApplicationComponentAtRootTag 并传入最初传递给 runApplication 的标签。这两个方法应始终成对使用。

AppRegistry 应在 require 序列中尽早引入,确保在加载其他模块前 JavaScript 执行环境已准备就绪。


参考

方法

getAppKeys()

tsx
static getAppKeys(): string[];

返回字符串数组。


getRegistry()

tsx
static getRegistry(): {sections: string[]; runnables: Runnable[]};

返回 Registry 对象。


getRunnable()

tsx
static getRunnable(appKey: string): : Runnable | undefined;

返回 Runnable 对象。

参数:

NameType
appKey
Required
string

getSectionKeys()

tsx
static getSectionKeys(): string[];

返回字符串数组。


getSections()

tsx
static getSections(): Record<string, Runnable>;

返回 Runnables 对象。


registerCancellableHeadlessTask()

tsx
static registerCancellableHeadlessTask(
taskKey: string,
taskProvider: TaskProvider,
taskCancelProvider: TaskCancelProvider,
);

注册可取消的无界面任务。无界面任务指无需用户界面即可运行的代码片段。

参数:

NameTypeDescription
taskKey
Required
stringThe native id for this task instance that was used when startHeadlessTask was called.
taskProvider
Required
TaskProviderA 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
TaskCancelProvidera 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()

tsx
static registerComponent(
appKey: string,
getComponentFunc: ComponentProvider,
section?: boolean,
): string;

参数:

NameType
appKey
Required
string
componentProvider
Required
ComponentProvider
sectionboolean

registerConfig()

tsx
static registerConfig(config: AppConfig[]);

参数:

NameType
config
Required
AppConfig[]

registerHeadlessTask()

tsx
static registerHeadlessTask(
taskKey: string,
taskProvider: TaskProvider,
);

注册无界面任务。无界面任务指无需用户界面即可运行的代码片段。

此方法可在应用处于后台时运行 JavaScript 任务,例如同步最新数据、处理推送通知或播放音乐。

参数:

NameTypeDescription
taskKey
Required
stringThe native id for this task instance that was used when startHeadlessTask was called.
taskProvider
Required
TaskProviderA 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()

tsx
static registerRunnable(appKey: string, func: Runnable): string;

参数:

NameType
appKey
Required
string
run
Required
function

registerSection()

tsx
static registerSection(
appKey: string,
component: ComponentProvider,
);

参数:

NameType
appKey
Required
string
component
Required
ComponentProvider

runApplication()

tsx
static runApplication(appKey: string, appParameters: any): void;

加载 JavaScript 包并运行应用。

参数:

NameType
appKey
Required
string
appParameters
Required
any

setComponentProviderInstrumentationHook()

tsx
static setComponentProviderInstrumentationHook(
hook: ComponentProviderInstrumentationHook,
);

参数:

NameType
hook
Required
function

有效的 hook 函数应接受以下参数:

NameType
component
Required
ComponentProvider
scopedPerformanceLogger
Required
IPerformanceLogger

该函数必须返回一个 React 组件。


setWrapperComponentProvider()

tsx
static setWrapperComponentProvider(
provider: WrapperComponentProvider,
);

参数:

NameType
provider
Required
ComponentProvider

startHeadlessTask()

tsx
static startHeadlessTask(
taskId: number,
taskKey: string,
data: any,
);

仅供原生代码调用。启动无界面任务。

参数:

NameTypeDescription
taskId
Required
numberThe native id for this task instance to keep track of its execution.
taskKey
Required
stringThe key for the task to start.
data
Required
anyThe data to pass to the task.

unmountApplicationComponentAtRootTag()

tsx
static unmountApplicationComponentAtRootTag(rootTag: number);

当视图需要销毁时停止应用程序。

参数:

NameType
rootTag
Required
number

类型定义

AppConfig

registerConfig 方法的应用程序配置。

Type
object

属性:

NameType
appKey
Required
string
componentComponentProvider
runfunction
sectionboolean

注意: 每个配置都应设置 componentrun 函数。

Registry

Type
object

属性:

NameType
runnablesarray of Runnables
sectionsarray of strings

Runnable

Type
object

属性:

NameType
componentComponentProvider
runfunction

Runnables

键为 appKey、值为 Runnable 类型的对象。

Type
object

Task

Task 是接受任意数据作为参数并返回解析为 undefined 的 Promise 的函数。

Type
function

TaskCanceller

TaskCanceller 是不接受参数且返回 void 的函数。

Type
function

TaskCancelProvider

有效的 TaskCancelProvider 是返回 TaskCanceller 的函数。

Type
function

TaskProvider

有效的 TaskProvider 是返回 Task 的函数。

Type
function