Skip to main content

Overview

The initExecutorch function initializes the React Native ExecuTorch library by configuring a resource fetcher adapter. This is a required setup step that must be called before using any hooks or modules.

Import

import { initExecutorch, ExecutorchConfig } from 'react-native-executorch';

Function Signature

function initExecutorch(config: ExecutorchConfig): void

Parameters

config
ExecutorchConfig
required
Configuration object containing the resource fetcher adapter

ResourceFetcherAdapter Interface

The resource fetcher adapter must implement the following interface:
fetch
function
required
Fetches resources (remote URLs, local files, or embedded assets) and stores them locally
fetch(
  callback: (downloadProgress: number) => void,
  ...sources: ResourceSource[]
): Promise<string[] | null>
readAsString
function
required
Reads file contents as a string
readAsString(path: string): Promise<string>

Usage Examples

Expo App

import { initExecutorch } from 'react-native-executorch';
import { ExpoResourceFetcher } from '@react-native-executorch/expo-resource-fetcher';

// Initialize at app startup (e.g., in App.tsx)
initExecutorch({
  resourceFetcher: new ExpoResourceFetcher(),
});

Bare React Native App

import { initExecutorch } from 'react-native-executorch';
import { BareResourceFetcher } from '@react-native-executorch/bare-resource-fetcher';

// Initialize at app startup (e.g., in index.js)
initExecutorch({
  resourceFetcher: new BareResourceFetcher(),
});

Custom Resource Fetcher

import { initExecutorch, ResourceFetcherAdapter } from 'react-native-executorch';

class CustomResourceFetcher implements ResourceFetcherAdapter {
  async fetch(
    callback: (progress: number) => void,
    ...sources: ResourceSource[]
  ): Promise<string[] | null> {
    // Custom implementation for fetching resources
    const paths: string[] = [];
    
    for (const source of sources) {
      // Download or locate resource
      const localPath = await this.downloadResource(source);
      paths.push(localPath);
      
      // Report progress
      callback(paths.length / sources.length);
    }
    
    return paths;
  }
  
  async readAsString(path: string): Promise<string> {
    // Custom implementation for reading files
    return await FileSystem.readAsStringAsync(path);
  }
  
  private async downloadResource(source: ResourceSource): Promise<string> {
    // Implementation details...
    return '/path/to/downloaded/file';
  }
}

initExecutorch({
  resourceFetcher: new CustomResourceFetcher(),
});

cleanupExecutorch

import { cleanupExecutorch } from 'react-native-executorch';

// Reset the resource fetcher adapter
cleanupExecutorch();
Cleans up the current resource fetcher setup. Primarily used for testing purposes.

Notes

You must call initExecutorch before using any hooks or modules in the library. Failure to initialize will result in a ResourceFetcherAdapterNotInitialized error.
Call initExecutorch only once at application startup. Multiple calls will override the previous configuration.

Error Handling

If you attempt to use any hook or module without initializing, you’ll receive:
ResourceFetcherAdapterNotInitialized: ResourceFetcher adapter is not initialized. 
Please call initExecutorch({ resourceFetcher: ... }) with a valid adapter.

See Also

Build docs developers (and LLMs) love