React Native Launch Arguments
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
-
Launch arguments are unexpectedly empty when using Expo.
cause Known Expo bug (#31830) regarding empty passed arguments or specific argument formatting.fixCheck Expo issue #31830 for latest status and workarounds. Ensure arguments are not empty or try passing a dummy argument to initialize. -
Launch arguments passed via `xcrun simctl launch` or Xcode Schemes are not received on iOS.
cause Incorrect formatting of arguments for iOS. Each argument must start with a hyphen.fixFor `xcrun simctl`, use `xcrun simctl launch booted com.MyAppBundleId -argName "argValue"`. In Xcode, ensure each argument in 'Arguments Passed On Launch' starts with a hyphen. -
App experiences a brief delay on Android startup before `LaunchArguments` are available or the app becomes interactive.
cause The module waits for the Android Activity to reach the `RESUMED` state due to a React Native core issue (#37518), which can introduce a small startup delay.fixThis is an internal workaround. Monitor React Native issue #37518 for a native fix that would allow this dependency to be removed.
Warnings
- gotcha On Android, the module may introduce minor delays in app loading time. This is because it force-waits for the Android activity to reach the `RESUMED` state, a workaround related to an outstanding React Native issue (#37518) regarding native module initialization.
- gotcha There is a known bug in Expo (issue #31830) where passing empty arguments to `react-native-launch-arguments` can result in unexpected behavior or failure to receive arguments correctly.
- gotcha When passing launch arguments on iOS, particularly via Xcode Schemes or `xcrun simctl`, each argument requires a preceding hyphen (e.g., `-hello "world"`). Incorrect formatting will prevent arguments from being received by the module.
Install
-
npm install react-native-launch-arguments -
yarn add react-native-launch-arguments -
pnpm add react-native-launch-arguments
Imports
- LaunchArguments
const { LaunchArguments } = require('react-native-launch-arguments');import { LaunchArguments } from 'react-native-launch-arguments'; - LaunchArguments.value()
const args = LaunchArguments.value();
- LaunchArguments.value<T>() (TypeScript)
interface MyArgs { userId?: string; debugMode?: boolean; }; const args = LaunchArguments.value<MyArgs>();
Quickstart
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.`);
}