跳到主要内容
新架构实战课 实操 + 基建 + 原理全维度包揽,抢先掌握 React Native 新架构精髓 立即查看 >Version: 0.71

AppRegistry

AppRegistry是所有 React Native 应用的 JS 入口。应用的根组件应当通过AppRegistry.registerComponent方法注册自己,然后原生系统才可以加载应用的代码包并且在启动完成之后通过调用AppRegistry.runApplication来真正运行应用。

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

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

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

要“结束”一个应用并销毁视图的话,请调用AppRegistry.unmountApplicationComponentAtRootTag方法,参数为在runApplication中使用的标签名。它们必须严格匹配。

AppRegistry应当在require序列中尽可能早的被 require 到,以确保 JS 运行环境在其它模块之前被准备好。


文档

方法

cancelHeadlessTask()

static cancelHeadlessTask(taskId, taskKey)

Only called from native code. Cancels a headless task. @param taskId the native id for this task instance that was used when startHeadlessTask was called @param taskKey the key for the task that was used when startHeadlessTask was called

参数:

名称类型说明
taskId
Required
numberThe native id for this task instance that was used when startHeadlessTask was called.
taskKey
Required
stringThe key for the task that was used when startHeadlessTask was called.

enableArchitectureIndicator()

static enableArchitectureIndicator(enabled)

参数:

名称类型
enabled
Required
boolean

getAppKeys()

static getAppKeys()

Returns an Array of AppKeys


getRegistry()

static getRegistry()

Returns a Registry object.


getRunnable()

static getRunnable(appKey)

Returns a Runnable object.

参数:

名称类型
appKey
Required
string

getSectionKeys()

static getSectionKeys()

Returns an array of strings.


getSections()

static getSections()

Returns a Runnables object.


registerCancellableHeadlessTask()

static registerCancellableHeadlessTask(taskKey, taskProvider, taskCancelProvider)

Register a headless task which can be cancelled. A headless task is a bit of code that runs without a UI.

参数:

名称类型说明
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()

static registerComponent(appKey, componentProvider, section?)

参数:

名称类型
appKey
Required
string
componentProvider
Required
ComponentProvider
sectionboolean

registerConfig()

static registerConfig(config)

参数:

名称类型
config
Required
AppConfig

registerHeadlessTask()

static registerHeadlessTask(taskKey, 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.

参数:

名称类型说明
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()

static registerRunnable(appKey, run)

参数:

名称类型
appKey
Required
string
run
Required
function

registerSection()

static registerSection(appKey, component)

参数:

名称类型
appKey
Required
string
component
Required
ComponentProvider

runApplication()

static runApplication(appKey, appParameters)

加载 JavaScript bundle 并运行应用。

参数:

名称类型
appKey
Required
string
appParameters
Required
any

setComponentProviderInstrumentationHook()

static setComponentProviderInstrumentationHook(hook)

参数:

名称类型
hook
Required
function

A valid hook function accepts the following as arguments:

名称类型
component
Required
ComponentProvider
scopedPerformanceLogger
Required
IPerformanceLogger

The function must also return a React Component.


setWrapperComponentProvider()

static setWrapperComponentProvider(provider)

参数:

名称类型
provider
Required
ComponentProvider

startHeadlessTask()

static startHeadlessTask(taskId, taskKey, data)

Only called from native code. Starts a headless task.

参数:

名称类型说明
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()

static unmountApplicationComponentAtRootTag(rootTag)

Stops an application when a view should be destroyed.

参数:

名称类型
rootTag
Required
number

类型定义

AppConfig

Application configuration for the registerConfig method.

类型
object

Properties:

名称类型
appKey
Required
string
componentComponentProvider
runfunction
sectionboolean

Note: Every config is expected to set either component or run function.

Registry

类型
object

Properties:

名称类型
runnablesarray of Runnables
sectionsarray of strings

Runnable

类型
object

Properties:

名称类型
componentComponentProvider
runfunction

Runnables

An object with key of appKey and value of type of Runnable.

类型
object

Task

A Task is a function that accepts any data as argument and returns a Promise that resolves to undefined.

类型
function

TaskCanceller

A TaskCanceller is a function that accepts no argument and returns void.

类型
function

TaskCancelProvider

A valid TaskCancelProvider is a function that returns a TaskCanceller.

类型
function

TaskProvider

A valid TaskProvider is a function that returns a Task.

类型
function