React Native Launch Arguments

4.1.1 · active · verified Sun Apr 19

The `react-native-launch-arguments` module provides a straightforward API for React Native applications to retrieve arguments passed during their launch. This capability is crucial for scenarios involving end-to-end testing, debugging, and advanced application configuration, allowing external tools like Detox, Appium, Maestro, or native debuggers (Xcode, `xcrun simctl`) to inject parameters directly into the running React Native app. Currently at version 4.1.1, the library typically updates as needed to maintain compatibility with new React Native releases and address platform-specific nuances. Its primary differentiation lies in offering a unified JavaScript interface to access platform-specific argument mechanisms on both iOS and Android, simplifying parameter passing that would otherwise require native bridge development. This facilitates robust testing and development workflows by externalizing configuration. It also ships with TypeScript types, enhancing developer experience for type-safe argument access.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and retrieve launch arguments with TypeScript, including defining expected argument types and conditional logic based on received values for common use cases like authentication and environment configuration.

import { LaunchArguments } from 'react-native-launch-arguments';

interface MyExpectedArgs {
  authToken?: string;
  skipAuth?: boolean;
  environment?: 'dev' | 'staging' | 'prod';
}

// Retrieve launch arguments with type safety
const args = LaunchArguments.value<MyExpectedArgs>();

console.log('Application launched with arguments:', args);

// Example of conditional logic based on launch arguments
if (args.skipAuth) {
  console.log('Authentication flow skipped as per launch arguments.');
} else {
  console.log('Authentication is required.');
}

// Simulate an API call that might use an auth token from launch args
const performAuthenticatedAction = async (token?: string) => {
  if (token) {
    console.log(`Using auth token (first 5 chars): ${token.substring(0, 5)}...`);
    // In a real app, this would be a network request with the token
    await new Promise(resolve => setTimeout(resolve, 1000));
    console.log('Authenticated action completed.');
  } else {
    console.warn('No authentication token provided via launch arguments.');
  }
};

performAuthenticatedAction(args.authToken);

if (args.environment) {
  console.log(`Running in ${args.environment} environment.`);
}

view raw JSON →