Set up React Native SDK

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
1import {
2 AutoEnvAttributes,
3 LDProvider,
4 ReactNativeLDClient,
5} from '@launchdarkly/react-native-client-sdk';
6
7const ldClient = new ReactNativeLDClient('YOUR_MOBILE_KEY', AutoEnvAttributes.Enabled, {
8 debug: true,
9 applicationInfo: {
10 id: 'ld-rn-test-app',
11 version: '0.0.1',
12 },
13});
14
15// A "context" is a data object representing users, devices, organizations, and other entities.
16const context = { kind: 'user', key: 'EXAMPLE_CONTEXT_KEY' };
17
18const App = () => {
19 useEffect(() => {
20 ldClient.identify(context);
21 }, []);
22
23 return (
24 <LDProvider client={ldClient}>
25 <YourComponent />
26 </LDProvider>
27 );
28};
29
30export default App;

You can find your server-side SDK key, client-side ID, and mobile key in the “Resources” section of the help menu. Click the help icon at the bottom left corner of the LaunchDarkly UI, then choose SDK keys:

The SDK keys option in the help menu.

The SDK keys option in the help menu.

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