跳到主要內容

AppRegistry

AppRegistry 是執行所有 React Native 應用程式的 JS 進入點。應用程式根組件應使用 AppRegistry.registerComponent 註冊自己,然後原生系統可以載入應用程式的 bundle,然後透過調用 AppRegistry.runApplication 在準備就緒時實際執行應用程式。

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

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

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

若要在視圖應該被銷毀時「停止」應用程式,請使用傳遞到 runApplication 的標籤調用 AppRegistry.unmountApplicationComponentAtRootTag。這些應始終成對使用。

應在 require 順序的早期要求 AppRegistry,以確保在要求其他模組之前設定 JS 執行環境。


參考

方法

getAppKeys()

tsx
static getAppKeys(): string[];

返回字串陣列。


getRegistry()

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

返回 Registry 物件。


getRunnable()

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

返回 Runnable 物件。

參數

名稱類型
appKey
必填
字串

getSectionKeys()

tsx
static getSectionKeys(): string[];

返回字串陣列。


getSections()

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

返回 Runnables 物件。


registerCancellableHeadlessTask()

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

註冊可以取消的無頭任務。無頭任務是一段在沒有 UI 的情況下運行的程式碼。

參數

名稱類型描述
taskKey
必填
字串當調用 startHeadlessTask 時使用的此任務實例的原生 ID。
taskProvider
必填
TaskProvider一個返回 Promise 的函數,它將從原生端傳遞的一些資料作為唯一的參數。當 Promise 被解決或拒絕時,原生端會收到此事件的通知,並且它可能會決定銷毀 JS 環境。
taskCancelProvider
必填
TaskCancelProvider一個返回 void 的函數,它不接受任何參數;當請求取消時,由 taskProvider 執行的函數應儘快結束並返回。

registerComponent()

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

參數

名稱類型
appKey
必填
字串
componentProvider
必填
ComponentProvider
section布林值

registerConfig()

tsx
static registerConfig(config: AppConfig[]);

參數

名稱類型
config
必填
AppConfig[]

registerHeadlessTask()

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

註冊無頭任務。無頭任務是一段在沒有 UI 的情況下運行的程式碼。

這是在您的應用程式在背景中時在 JavaScript 中運行任務的一種方法。例如,它可以用於同步最新資料、處理推播通知或播放音樂。

參數

名稱類型描述
taskKey
必填
字串當調用 startHeadlessTask 時使用的此任務實例的原生 ID。
taskProvider
必填
TaskProvider一個返回 Promise 的函數,它將從原生端傳遞的一些資料作為唯一的參數。當 Promise 被解決或拒絕時,原生端會收到此事件的通知,並且它可能會決定銷毀 JS 環境。

registerRunnable()

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

參數

名稱類型
appKey
必填
字串
run
必填
函數

registerSection()

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

參數

名稱類型
appKey
必填
字串
component
必填
ComponentProvider

runApplication()

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

載入 JavaScript bundle 並運行應用程式。

參數

名稱類型
appKey
必填
字串
appParameters
必填
任何

setComponentProviderInstrumentationHook()

tsx
static setComponentProviderInstrumentationHook(
hook: ComponentProviderInstrumentationHook,
);

參數

名稱類型
hook
必填
函數

有效的 hook 函數接受以下參數

名稱類型
component
必填
ComponentProvider
scopedPerformanceLogger
必填
IPerformanceLogger

該函數也必須返回一個 React 組件。


setWrapperComponentProvider()

tsx
static setWrapperComponentProvider(
provider: WrapperComponentProvider,
);

參數

名稱類型
provider
必填
ComponentProvider

startHeadlessTask()

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

僅從原生程式碼調用。啟動無頭任務。

參數

名稱類型描述
taskId
必填
數字用於追蹤其執行的此任務實例的原生 ID。
taskKey
必填
字串要啟動的任務的 key。
data
必填
任何要傳遞給任務的資料。

unmountApplicationComponentAtRootTag()

tsx
static unmountApplicationComponentAtRootTag(rootTag: number);

當視圖應該被銷毀時停止應用程式。

參數

名稱類型
rootTag
必填
數字

類型定義

AppConfig

registerConfig 方法的應用程式配置。

類型
物件

屬性

名稱類型
appKey
必填
字串
componentComponentProvider
run函數
section布林值

注意: 每個配置都應設定 componentrun 函數。

Registry

類型
物件

屬性

名稱類型
runnablesRunnables 陣列
sections字串陣列

Runnable

類型
物件

屬性

名稱類型
componentComponentProvider
run函數

Runnables

一個物件,其 key 為 appKey,值為 Runnable 類型。

類型
物件

Task

Task 是一個函數,它接受任何資料作為參數,並返回一個解析為 undefined 的 Promise。

類型
函數

TaskCanceller

TaskCanceller 是一個不接受任何參數並返回 void 的函數。

類型
函數

TaskCancelProvider

有效的 TaskCancelProvider 是一個返回 TaskCanceller 的函數。

類型
函數

TaskProvider

有效的 TaskProvider 是一個返回 Task 的函數。

類型
函數