Set up React Native SDK

Install and initialize the LaunchDarkly React Native SDK to start evaluating feature flags in your application.

Install the package

yarn add @launchdarkly/react-native-client-sdk

LaunchDarkly’s React Native SDK v10 uses @react-native-async-storage/async-storage for bootstrapping. If you are not using Expo, you must explicitly add it as a dependency.

yarn add @react-native-async-storage/async-storage

Initialize the SDK

React Native SDK initialization
import {
AutoEnvAttributes,
LDProvider,
ReactNativeLDClient,
} from '@launchdarkly/react-native-client-sdk';
// This is your mobile key.
const ldClient = new ReactNativeLDClient('YOUR_MOBILE_KEY', 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);
}, []);
return (
<LDProvider client={ldClient}>
<YourComponent />
</LDProvider>
);
};
export default App;

Initialize the SDK for feature management and Experimentation

If you want to use the LaunchDarkly Experimentation feature, include a track call in your initialization code:

React Native SDK initialization for Experimentation
import React, { useEffect, useState, useCallback } from 'react';
import {
AutoEnvAttributes,
LDProvider,
ReactNativeLDClient,
} from '@launchdarkly/react-native-client-sdk';
// This is your mobile key.
const ldClient = new ReactNativeLDClient('YOUR_MOBILE_KEY', 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', email: 'EXAMPLE_EMAIL' };
export default function App() {
const [ready, setReady] = useState<boolean>(false);
// Wait for identify to complete before rendering, so no flag evaluates
// against defaults during startup.
useEffect(() => {
(async () => {
await ldClient.identify(context);
setReady(true);
})();
}, []);
// Call trackMetric when a metric action occurs in your app —
// a tap, a form submit, a screen view, a custom event, whatever your metric measures.
const trackMetric = useCallback(
(metricKey: string, data?: unknown) => {
ldClient.track(metricKey, data);
},
[],
);
if (!ready) return null;
return (
<LDProvider client={ldClient}>
<YourComponent />
</LDProvider>
);
}

You can find your server-side SDK keys, mobile keys, and client-side ID on the SDK keys page under Settings.

To learn more, read Initialize the client and identify a context in the React Native SDK reference guide.