Optimizely will be sunsetting Full Stack Experimentation on July 29, 2024. See the recommended Feature Experimentation migration timeline and documentation.

Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySumbit a ticketLog In
GitHubNuGetDev CommunitySumbit a ticket

OptimizelyVariation

This topic describes the OptimizelyVariation, which is used with a parent OptimizelyExperiment to render different content for different variations.

Version

SDK v1.0.0

Description

The OptimizelyExperiment component buckets a user into one of the variations defined on the experiment and passes the variation_key of the variation assigned to the children component. The OptimizelyVariation component labeled with the variation prop equal to the variation_key will be rendered inside the OptimizelyExperiment component.

Props

The table below lists the required and optional props for the OptimizelyExperiment Component in React.

ParameterTypeDescription
variationstringThe key of the variation for which content should be rendered. Variation keys are in the experiment details page.
default
optional
booleanWhen true, content will be rendered in the default case (null variation returned from the OptimizelyExperiment)
childrenReact.ReactNode | FunctionContent or function returning content to be rendered based on the enabled status and variable values of the feature. See usage examples below.

Examples

If your experiment key was exp1 and that experiment had a variation simple and detailed, you could use the ExperimentComponent to render a SimpleComponent or a DetailedComponent depending on whether the user was assigned the 'simple' variation or 'detailed' variation. If for some reason Optimizely did not initialize in the 3000 ms timeout, then the default variation would be rendered instead:

import {
  OptimizelyExperiment,
  OptimizelyVariation
} from '@optimizely/react-sdk'

function ExperimentComponent() {
  return (
    <OptimizelyExperiment experiment="exp1" timeout={3000}>
      <OptimizelyVariation variation="simple">
        <SimpleComponent />
      </OptimizelyVariation>

      <OptimizelyVariation variation="detailed">
        <ComplexComponent />
      </OptimizelyVariation>

      <OptimizelyVariation default>
        <SimpleComponent />
      </OptimizelyVariation>
    </OptimizelyExperiment>
  )
}

Source files

The language/platform source files containing the implementation for React is index.ts.