This page describes how to instrument your React 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 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 components.
Installation
Install the Datadog OpenFeature provider and the OpenFeature React SDK using your preferred package manager:
Create a DatadogProvider instance and register it with OpenFeature. Do this as early as possible in your application, before rendering your React components.
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.
Set the provider along with the evaluation context:
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.
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{useBooleanFlag}from'@openfeature/react-sdk';import{Suspense}from'react';importfunctionContent(){// 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}=useBooleanFlag('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 application:
import{Suspense}from'react';import{DatadogProvider}from'@datadog/openfeature-browser';import{OpenFeatureProvider,OpenFeature,useBooleanFlagValue}from'@openfeature/react-sdk';// Initialize the Datadog provider
constprovider=newDatadogProvider({applicationId:'<APPLICATION_ID>',clientToken:'<CLIENT_TOKEN>',env:'<ENV_NAME>',});// Set the evaluation context
constevaluationContext={targetingKey:'user-123',user_id:'123',user_role:'admin',};OpenFeature.setProvider(provider,evaluationContext);// Wrap your app with the OpenFeatureProvider and Suspense for loading state
functionApp(){return(<Suspensefallback={<Loading/>}><OpenFeatureProvidersuspendUntilReady><Page/></OpenFeatureProvider></Suspense>);}// Use feature flags in your components
functionPage(){constshowNewFeature=useBooleanFlagValue('new_feature',false);return(<div>{showNewFeature?<NewFeature/>:<ExistingFeature/>}</div>);}
Update the evaluation context
To update the evaluation context after initialization (for example, when a user logs in), use OpenFeature.setContext():
// When a user logs in
awaitOpenFeature.setContext({targetingKey:user.id,user_id:user.id,email:user.email,plan:user.plan,});
Further reading
Additional helpful documentation, links, and articles: