Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Example usage of the React SDK

Code example of how to use the Optimizely Feature Experimentation React SDK to send Decision events, make flag decisions and implement the Track method.


Before using the client to evaluate flag rules, including A/B tests and flag deliveries, you must:

  1. Install the React SDK.
  2. Initialize the React SDK.

Example React SDK implementation

This example demonstrates the basic usage of each of these concepts:

  1. Evaluate a flag with the key product_sort using the Decision hook. As a side effect, the Decision hook also sends a decision event to Optimizely Feature Experimentation to record that the current user has been exposed to the experiment.

  2. Conditionally execute your feature code. You have a couple of options:

    1. Fetch the flag-enabled state, then check a configuration variable on the flag called sort_method. The SDK evaluates your flag rules and determines what flag variation the user is in and which sort method variable they should see.
    2. Fetch on the flag variation, then run the 'control' or 'treatment' code.
  3. Use event tracking to track an event called purchased. This conversion event measures the impact of an experiment. Using the Track method, the purchase is automatically attributed back to the running A/B test for which we made a decision, and the SDK sends a network request to Optimizely Feature Experimentation through the customizable event dispatcher, so Optimizely can count it in your results page.

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

const optimizely = createInstance({
  sdkKey: '<Your_SDK_Key>', // TODO: Update to your SDK Key

function Button(props) {
  const handleClick = () => {
    const { optimizely } = props;    
  return <button onClick={handleClick}>Purchase</button>;

const PurchaseButton = withOptimizely(Button)

function ProductSort() {
  const [ decision ] = useDecision('product_sort');
  const variationKey = decision.variationKey;
  const isEnabled = decision.enabled;
  const sortMethod = decision.variables['sort_method'];
  return (
      { /* If variation is null, display error */ }
      { variationKey === null && <div className="error">{ decision.reasons }</div> }

      { /* If feature is enabled, do something with the Sort Method variable */ }
      { isEnabled && <div>The Sort method is {sortMethod}</div> }
      { /* Show relevant component based on the variation key */ }
      { variationKey === 'control' && <div>Control Component</div> }
      { variationKey === 'treatment' && <div>Treatment Component</div> }
      { /* Show Purchase Button to track Purchase event */ }
      <PurchaseButton />

function App() {
  return (
    <OptimizelyProvider optimizely={optimizely} user={{ id: 'user123' }}>
      <ProductSort />

export default App;


For a more step-by-step example, see the React SDK quickstart: