Subchapter 50.25
references/sdk/snippets/react-native-sdk.mdMarkdown3 KBView on GitHub
Current SDK: @launchdarkly/react-native-client-sdk v10 (TypeScript, Expo-compatible (opens in a new tab); iOS and Android only—not web). Build ReactNativeLDClient with the mobile key, wrap the app in LDProvider, and call identify(context) after mount (no context at construction). identify uses a 5s timeout by default; do not strip timeouts in custom configuration.
Install (non-Expo): Add @react-native-async-storage/async-storage, then run npx pod-install for iOS (Install the SDK (opens in a new tab)).
Mobile key: Resolve a non-empty string at runtime (below uses Expo’s EXPO_PUBLIC_ env). Bare React Native typically uses react-native-config or native build settings—never hardcode keys in source. Logical name: Apply: environment configuration.
Includes: Copy-paste onboarding sample below.
import { useEffect } from 'react';
import {
AutoEnvAttributes,
LDProvider,
ReactNativeLDClient,
} from '@launchdarkly/react-native-client-sdk';
const mobileKey = process.env.EXPO_PUBLIC_LAUNCHDARKLY_MOBILE_KEY?.trim();
if (!mobileKey) {
throw new Error(
'LaunchDarkly: missing mobile key. For Expo, set EXPO_PUBLIC_LAUNCHDARKLY_MOBILE_KEY. For bare React Native, inject the key (for example react-native-config) and pass it here.',
);
}
const ldClient = new ReactNativeLDClient(mobileKey, AutoEnvAttributes.Enabled, {
debug: true,
applicationInfo: {
id: 'ld-rn-test-app',
version: '0.0.1',
},
});
// A "context" is a data object representing users, devices, organizations, and other entities.
const context = { kind: 'user', key: 'EXAMPLE_CONTEXT_KEY' };
const App = () => {
useEffect(() => {
ldClient.identify(context).catch((e: unknown) => {
console.error('LaunchDarkly identify failed:', e);
});
}, []);
return (
<LDProvider client={ldClient}>
<YourComponent />
</LDProvider>
);
};
export default App;Expo requires the EXPO_PUBLIC_ prefix for process.env in app code; other setups should substitute their own resolved string for mobileKey.