This product is not supported for your selected Datadog site. ().
Overview
This page describes how to instrument your React Native application with the Datadog Feature Flags SDK. Datadog feature flags provide a unified way to remotely control feature availability in your app, experiment safely, and deliver new experiences with confidence.
The Datadog Feature Flags SDK for React Native is built on OpenFeature, an open standard for feature flag management. This guide explains how to install the SDK, configure the Datadog provider, and evaluate flags in your React Native components.
Requirements
React Native version 0.65 or later
iOS version 13 or later
Android API level 23 or later
Datadog React Native SDK (@datadog/mobile-react-native) must be initialized first
Installation
Install the Datadog React Native SDK, the OpenFeature provider, and the OpenFeature React SDK using your preferred package manager:
If you use React Native version 0.68 or higher, use Java 17. If you use React Native version 0.67 or lower, use Java version 11.
In your android/build.gradle file, specify the kotlinVersion to avoid clashes among Kotlin dependencies:
build.gradle
buildscript{ext{kotlinVersion="1.8.21"}}
Initialize the SDK
The Datadog OpenFeature provider for React Native requires the core Datadog SDK to be initialized first, followed by enabling the Feature Flags feature.
Option 1: Using DatadogProvider component
If you use the DatadogProvider component for SDK initialization, enable Feature Flags in the onInitialized callback:
If you initialize the SDK imperatively, enable Feature Flags after initialization completes:
import{DdSdkReactNative,DdFlags,CoreConfiguration}from'@datadog/mobile-react-native';import{DatadogOpenFeatureProvider}from'@datadog/mobile-react-native-openfeature';import{OpenFeature}from'@openfeature/react-sdk';(async()=>{constconfig=newCoreConfiguration('<CLIENT_TOKEN>','<ENVIRONMENT_NAME>','<APPLICATION_ID>');config.site='';awaitDdSdkReactNative.initialize(config);// Enable Feature Flags after core SDK initialization
awaitDdFlags.enable();// Set the Datadog provider with OpenFeature
constprovider=newDatadogOpenFeatureProvider();OpenFeature.setProvider(provider);})();
Sending flag evaluation data to Datadog is automatically enabled when using the Feature Flags SDK. Provide rumIntegrationEnabled and trackExposures parameters to the DdFlags.enable() call to configure this behavior.
Set the evaluation context
Define who or what the flag evaluation applies to using an evaluation context. The evaluation context includes user or session information used to determine which flag variations should be returned. Reference these attributes in your targeting rules to control who sees each variant.
The targetingKey is used as the randomization subject for percentage-based targeting. When a flag targets a percentage of subjects (for example, 50%), the targetingKey determines which "bucket" a user falls into. Users with the same targetingKey always receive the same variant for a given flag.
Wrap your application
Wrap your application with the OpenFeatureProvider component. This makes feature flags available to all child components through React context.
The OpenFeature React SDK provides hooks for evaluating flags within your components. Each hook returns the flag value based on the evaluation context you configured. Flag evaluation is local and instantaneous—the SDK uses locally cached data, so no network requests occur when evaluating flags.
Boolean flags
Use useBooleanFlagValue(key, defaultValue) for flags that represent on/off or true/false conditions:
Built-in suspense support allows you to avoid displaying components with feature flags until provider initialization is complete, or when the context changes. Pass { suspend: true } in the hook options to use this functionality.
For example:
import{useBooleanFlagValue}from'@openfeature/react-sdk';import{Suspense}from'react';functionContent(){// Display a loading message if the component uses feature flags and the provider is not ready
return(<Suspensefallback={"Loading..."}><WelcomeMessage/></Suspense>);}functionWelcomeMessage(){const{value:showNewMessage}=useBooleanFlagValue('show-new-welcome-message',false,{suspend:true});return(<>{showNewMessage?(<p>Welcome!You'reseeingthenewexperience.</p>):(<p>Welcomeback!</p>)}</>);}
Flag evaluation details
When you need more than just the flag value, use the detail hooks. These return both the evaluated value and metadata explaining the evaluation:
useBooleanFlagDetails(key, defaultValue)
useStringFlagDetails(key, defaultValue)
useNumberFlagDetails(key, defaultValue)
useObjectFlagDetails(key, defaultValue)
For example:
import{useStringFlagDetails}from'@openfeature/react-sdk';functionPaywallLayout(){constdetails=useStringFlagDetails('paywall_layout','control');console.log(details.value);// Evaluated value (for example: "A", "B", or "control")
console.log(details.variant);// Variant name, if applicable
console.log(details.reason);// Description of why this value was chosen
return<Layoutvariant={details.value}/>;}
Flag details help you debug evaluation behavior and understand why a user received a given value.
Complete example
Here’s a complete example showing how to set up and use Datadog Feature Flags in a React Native application:
import{Suspense}from'react';import{View}from'react-native';import{DatadogProvider,DatadogProviderConfiguration,DdFlags}from'@datadog/mobile-react-native';import{DatadogOpenFeatureProvider}from'@datadog/mobile-react-native-openfeature';import{OpenFeature,OpenFeatureProvider,useBooleanFlagValue}from'@openfeature/react-sdk';constconfig=newDatadogProviderConfiguration('<CLIENT_TOKEN>','<ENVIRONMENT_NAME>',trackingConsent,{rumConfiguration:{applicationId:'<APPLICATION_ID>',},// ...
},);// Wrap your app with the DatadogProvider and OpenFeatureProvider.
exportdefaultfunctionAppWithProviders() {// Based on your auth state.
constuser=getCurrentUser();return(<DatadogProviderconfiguration={config}onInitialized={async()=>{awaitDdFlags.enable();constprovider=newDatadogOpenFeatureProvider();constevaluationContext={targetingKey: user.id,region: user.country,};OpenFeature.setProvider(provider,evaluationContext);}}><Suspensefallback={<Loading/>}><OpenFeatureProvidersuspendUntilReady><App/></OpenFeatureProvider></Suspense></DatadogProvider>);}// Use feature flags in your components
functionApp() {constshowNewFeature=useBooleanFlagValue('new_feature',false);return(<View>{showNewFeature?<NewFeature/>:<ExistingFeature/>}</View>);}
Update the evaluation context
To update the evaluation context after initialization (for example, when a user logs in), use OpenFeature.setContext():
import{OpenFeature}from'@openfeature/react-sdk';// When a user logs in
functiononUserLogin(user){OpenFeature.setContext({targetingKey: user.id,email: user.email,plan: user.plan,});}
Configure Feature Flags options
Pass configuration options to DdFlags.enable() to customize Feature Flags behavior:
awaitDdFlags.enable({// Send flag evaluation data to RUM for session analysis (default: true)
rumIntegrationEnabled: true,// Track flag exposures for analytics (default: true)
trackExposures: true,});
rumIntegrationEnabled
When true (default), flag evaluations are tracked in RUM, which enables correlating them with user sessions. This enables analytics such as “Do users in variant B experience more errors?”.
trackExposures
When true (default), the SDK automatically records an exposure event when a flag is evaluated. These events contain metadata about which flag was accessed, which variant was served, and under what context. They are sent to Datadog so you can analyze feature adoption.
Context attribute requirements
Evaluation context attributes must be flat primitive values (strings, numbers, Booleans). Nested objects and arrays are not supported and will be dropped from the evaluation context.
// These attributes will be dropped from the evaluation context with a console warning.
OpenFeature.setContext({targetingKey:'user-123',user:{id:'user-123'},// nested object - NOT SUPPORTED
features:['beta','analytics']// array - NOT SUPPORTED
});
Troubleshooting
No flags returned
If flag evaluations return default values:
Verify the Datadog SDK is initialized before calling DdFlags.enable().
Confirm DdFlags.enable() completed before setting the OpenFeature provider.
Check that the evaluation context is set before evaluating flags.
Confirm the flag exists and is enabled in your Datadog Feature Flags dashboard.
iOS pod install errors
If you have use_frameworks! enabled in your Podfile, you may see errors during pod install. Edit your Podfile to install the SDK pod as a static library: