@datadog/mobile-react-native-openfeature
v3.1.2
Published
A client-side React Native module to provide OpenFeature integration with Datadog Feature Flags
Maintainers
Readme
Datadog OpenFeature Provider for React Native
Use OpenFeature with Datadog Feature Flags to evaluate feature flags and send evaluation data to Datadog for analysis and experimentation.
OpenFeature is a vendor-neutral, community-driven specification and SDK for feature flagging. It provides a unified API for feature flag evaluation that works across different providers. This enables you to switch vendors or integrate multiple feature flag systems.
This package provides an OpenFeature-compatible provider that wraps Datadog's Feature Flags SDK.
Setup
Note: This package is an integration for the OpenFeature React SDK. Install and set up the core @datadog/mobile-react-native SDK to start using Datadog Feature Flags.
To install with NPM, run:
npm install @datadog/mobile-react-native @datadog/mobile-react-native-openfeature @openfeature/react-sdkTo install with Yarn, run:
yarn add @datadog/mobile-react-native @datadog/mobile-react-native-openfeature @openfeature/react-sdkUsage
Initialize the Datadog SDK and OpenFeature
Use the following example code snippet to initialize the Datadog SDK, enable the Feature Flags feature, and set up the OpenFeature provider.
import { CoreConfiguration, DatadogProvider, DdFlags } from '@datadog/mobile-react-native';
import { DatadogOpenFeatureProvider } from '@datadog/mobile-react-native-openfeature';
import { OpenFeature } from '@openfeature/react-sdk';
(async () => {
// Follow the core Datadog SDK initialization guide.
const config = new CoreConfiguration(
// ...
);
await DdSdkReactNative.initialize(config);
// Enable Datadog Flags feature after the core SDK has been initialized.
await DdFlags.enable();
// Set the Datadog provider with OpenFeature.
const provider = new DatadogOpenFeatureProvider();
OpenFeature.setProvider(provider);
})();
// Alternatively, if using `<DatadogProvider />` for core SDK initialization.
<DatadogProvider
configuration={coreConfiguration}
onInitialized={async () => {
await DdFlags.enable();
const provider = new DatadogOpenFeatureProvider();
OpenFeature.setProvider(provider);
}}
>
{/* ... */}
</DatadogProvider>After completing this setup, your app is ready for flag evaluation with OpenFeature.
Note: Sending flag evaluation data to Datadog is automatically enabled when using the Feature Flags SDK. Provide
rumIntegrationEnabledandtrackExposuresparameters to theDdFlags.enable()call to configure.
Using the OpenFeature React SDK
For complete details on using the OpenFeature React SDK, including flag evaluation, evaluation context management, and advanced setup options, see the OpenFeature React SDK documentation.
Short-form OpenFeature SDK usage example:
import { OpenFeature, OpenFeatureProvider, useFlag } from '@openfeature/react-sdk';
function AppWithProviders() {
// For advanced feature flag targeting based on current user or device.
useEffect(() => {
const user = { ... }; // Obtained from your authentication logic.
OpenFeature.setContext({
// User or anonymous ID for consistent feature flag evaluations.
targetingKey: user.id,
// Properties for more granular targeting.
region: user.country
});
}, [])
// Wrap your app with OpenFeatureProvider to allow flag evaluations throughout the app.
return (
<OpenFeatureProvider>
<App />
</OpenFeatureProvider>
);
}
function App() {
const { value: isNewFeatureEnabled } = useFlag('new-feature-enabled', false);
return (
<View>
{isNewFeatureEnabled && <NewFeatureComponent />}
{/* ... */}
</View>
)
}
export default AppWithProviders;