The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.
Dev guideRecipesAPI ReferenceChangelog
Dev guideAPI ReferenceRecipesChangelogUser GuideGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Real-Time Audiences for Feature Experimentation segment qualification methods for the React Native SDK

How the React Native SDK handles ODP audience segments automatically, and how to use segment qualification methods for advanced use cases.

Prerequisites

You must enable and configure Real-Time Audiences for Feature Experimentation before using audience segment features.

Automatic segment fetching

When you enable ODP by passing createOdpManager() to createInstance, the React Native SDK automatically fetches qualified segments for the user when the <OptimizelyProvider> creates the user context. You do not need to manually call fetchQualifiedSegments in your components — the decide hooks (useDecide, etc.) already have access to the fetched segments when making decisions.

import {
  createInstance,
  createPollingProjectConfigManager,
  createBatchEventProcessor,
  createOdpManager,
  OptimizelyProvider,
  useDecide,
} from '@optimizely/react-sdk';

const optimizely = createInstance({
  projectConfigManager: createPollingProjectConfigManager({
    sdkKey: 'YOUR_SDK_KEY',
  }),
  eventProcessor: createBatchEventProcessor(),
  odpManager: createOdpManager(),
});

function App() {
  return (
    <OptimizelyProvider client={optimizely} user={{ id: 'user-123' }}>
      <MyComponent />
    </OptimizelyProvider>
  );
}

function MyComponent() {
  // Segments are already fetched automatically — decisions use them
  const { decision, isLoading, error } = useDecide('my-flag');

  if (isLoading) return <Text>Loading...</Text>;
  if (error) return <Text>Error: {error.message}</Text>;

  return <Text>{decision.enabled ? 'Feature on' : 'Feature off'}</Text>;
}

Checking segment qualification

If you need to check whether the user is qualified for a specific segment (for example, for analytics or conditional rendering), use isQualifiedFor() on the user context:

import { useOptimizelyUserContext } from '@optimizely/react-sdk';

function SegmentDebug() {
  const { userContext, isLoading } = useOptimizelyUserContext();

  if (isLoading || !userContext) return null;

  const isQualified = userContext.isQualifiedFor('segment1');

  return <Text>Is qualified for segment1: {isQualified ? 'Yes' : 'No'}</Text>;
}

Segment caching

Fetched segments are cached in memory. The cache is configured when creating the ODP manager:

  • Cache size — Default: 100. Set to 0 to disable caching.
  • Cache timeout — Default: 600,000 milliseconds (10 minutes). Set to 0 to disable timeout (never expires).
const odpManager = createOdpManager({
  segmentsCacheSize: 100,
  segmentsCacheTimeout: 600000, // 10 minutes in milliseconds
});

The cache uses the least recently used (LRU) algorithm, so the oldest record is evicted when the maximum size is reached.

fetchQualifiedSegments

For advanced use cases where you need to manually fetch segments (for example, outside of a React component tree), you can create a user context directly on the client and fetch segments before making decisions:

import {
  createInstance,
  createPollingProjectConfigManager,
  createBatchEventProcessor,
  createOdpManager,
} from '@optimizely/react-sdk';

const optimizely = createInstance({
  projectConfigManager: createPollingProjectConfigManager({
    sdkKey: 'YOUR_SDK_KEY',
  }),
  eventProcessor: createBatchEventProcessor(),
  odpManager: createOdpManager(),
});

async function fetchAndDecide() {
  await optimizely.onReady();

  const userContext = optimizely.createUserContext('user-123');
  await userContext.fetchQualifiedSegments();

  const decision = userContext.decide('my-flag');
  return decision;
}

Parameters

ParameterTypeDescription
options (optional)OptimizelySegmentOption[]A set of options for fetching qualified segments from ODP.

Returns

Promise<boolean> — resolves with true if segments are fetched successfully, false otherwise.

Cache bypass options

  • OptimizelySegmentOption.IGNORE_CACHE — Bypass segments cache for lookup and save.
  • OptimizelySegmentOption.RESET_CACHE — Reset all segments cache.

isQualifiedFor

Checks if the user is qualified for the given audience segment.

Parameters

ParameterTypeDescription
segmentstringThe ODP audience segment name to check.

Returns

true if the user is qualified, otherwise false.

Source files

The language and platform source files containing the implementation for the React Native SDK are available on GitHub.