React Native SDK reference
Read time: 7 minutes
Last edited: Dec 10, 2024
Version 10 of the React Native SDK has been rewritten in pure TypeScript and is compatible with Expo. Only iOS and Android platforms are supported. Web is not supported. Version 10 is a rewrite of the React Native SDK. It is based on the JavaScript SDK, and it replaces launchdarkly-react-native-client-sdk. To learn more about upgrading, read React Native SDK 9.x to 10.0 migration guide.
Version 9 of the React Native SDK includes updated dependencies. If you are upgrading from version 8 or below, we recommend upgrading directly to version 10. To learn more about version 9 specifically, read React Native SDK 8.x to 9.0 migration guide.
Version 8 of the React Native SDK introduces optional automatic collection of environment attributes. To learn more about upgrading, read React Native SDK 7.x to 8.0 migration guide.
Overview
This topic documents how to get started with the React Native SDK, and links to reference information on all of the supported features.
LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications:
Resource | Location |
---|---|
SDK API documentation | SDK API docs |
GitHub repository | react-native |
Sample Expo application | React Native |
Published module | npm |
Get started
After you complete the Get started process, follow these instructions to start using the LaunchDarkly SDK in your React Native code:
Understand version compatibility
The LaunchDarkly React Native client-side SDK version 10.x is compatible with Expo. Only iOS and Android platforms are supported. Web is not supported.
Earlier versions of the LaunchDarkly React Native SDK are not compatible with the Expo managed workflow because they use native modules. Consider upgrading to the 10.x version of the LaunchDarkly SDK, or using the bare workflow instead.
The following table describes the LaunchDarkly React Native SDK version compatibility:
LaunchDarkly React Native SDK version | Compatible with |
---|---|
10.x | React Native 0.72, 0.73, 0.74, 0.76; Expo 51, Expo 52 |
9.x | React Native 0.73 |
8.x | React Native 0.69 through 0.72 |
7.x | React Native 0.69 |
6.x | Xcode 12.2 or higher and React Native 0.64 |
If you need support for earlier versions of React Native, use older versions of the LaunchDarkly React Native SDK. To learn more, read Releases.
Install the SDK
To install the SDK:
- Install the LaunchDarkly SDK as a dependency.
- The LaunchDarkly React Native SDK version 10 uses
@react-native-async-storage/async-storage
for bootstrapping:
-
If you are using Expo, skip this step and continue to step 3.
-
If you are not using Expo, you must explicitly add
@react-native-async-storage/async-storage
as a dependency to your project.
-
Run
npx pod-install
. -
Next, import the SDK into your project:
The React Native SDK uses a mobile key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.
Mobile keys are not secret and you can expose them in your client-side code without risk. However, never embed a server-side SDK key into a client-side application.
-
After the SDK is imported, create a single, shared instance of
ReactNativeLDClient
. To create this, you need your environment's mobile key. This authorizes your application to connect to a particular environment within LaunchDarkly. -
After you instantiate
ReactNativeLDClient
, pass it toLDProvider
.LDProvider
uses the React context API to store and pass data to child components through hooks. It requires the client.
Initialize the client and identify a context
In version 10 of the SDK, you do not specify a context when you initialize the client. Instead, you must provide the context in an identify()
call, for example on application mount. The identify()
method returns a promise that can be awaited. End users will receive fallback values until you specify a context by calling identify()
.
Do not configure your SDK to initialize without a timeout parameter. Doing so will cause your app never to load if there is a connectivity problem. We recommend setting a timeout for no more than 1-5 seconds.
In v10, the identify
timeout defaults to 5 seconds. In older versions, the client.configure
takes a timeout parameter.
The following example shows the simplest way to create a shared instance of ReactNativeLDClient
, and identify a context:
To learn more about the specific configuration options available in this SDK, read LDOptions
.
We recommend making ReactNativeLDClient
a singleton for each LaunchDarkly project. The client instance maintains internal state that allows LaunchDarkly to serve feature flags without making any remote requests.
You must make feature flags available to mobile SDKs before the SDK can evaluate those flags. If an SDK tries to evaluate a feature flag that is not available, LaunchDarkly serves the fallback value for that flag.
To make a flag available to this SDK, check the SDKs using Mobile key checkbox during flag creation, or on the flag's settings page. To make all of a project's flags available to this SDK by default, check the SDKs using Mobile key checkbox on your project's Flag settings page.
Background fetch
In version 10, the React Native SDK defaults to a streaming
connection mode. In this mode, your application receives updates when it is in the foreground and does not when it is in the background. To learn more, read Offline mode.
In previous versions, the SDK defaulted to the background fetch behavior for the platform it was running on.
Shut down the client
Shut down the client when your application terminates. To learn more, read Shutting down.
Data collection
To learn more about data collection within this SDK and implications on submissions to the Apple App Store, read Apple App Store data collection policy.
Supported features
This SDK supports the following features:
- Anonymous contexts and users
- Automatic environment attributes
- Configuration, including
- Context configuration
- Evaluating flags
- Flag evaluation reasons
- Flushing events
- Getting all flags
- Identifying and changing contexts
- Logging
- Monitoring SDK status
- Multiple environments
- Offline mode
- Private attributes
- Relay Proxy configuration, using proxy mode
- Sending custom events
- Shutting down
- Subscribing to flag changes