HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

OptimizelyProvider

This topcic describes how to wrap your React application at the root with OptimizelyProvider to give your entire React application access to Optimizely's APIs.

Wrap your React application at the root with OptimizelyProvider to give your entire React application access to Optimizely's APIs.

Version

SDK v1.0.0

Description

The OptimizelyProvider leverages React’s Context API to allow access to the ReactSDKClient to useDecision hook.

Props

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

ParameterTypeDescription
optimizelyReactSDKClientOptimizely instance created from calling createInstance
userobject: { id: string; attributes?: { [key: string]: any } } | Promise User infoThe user id and user attributes to be passed to the SDK for every feature flag, A/B test, or track call, or a Promise for the same kind of object.
timeout
optional
numberThe amount of time for useDecision hook to return null flag Decision while waiting for the SDK instance to become ready, before resolving.
isServerSide
optional
booleanmust pass true here for server side rendering

Examples

If the root component of your application was , wrap your App with the OptimizelyProvider component. Pass the result of calling createInstance to the optimizely prop and set the user object with id and attributes identifying the user:

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

const optimizely = createInstance({
  sdkKey: '<Your_SDK_Key>',
})

class AppWrapper extends React.Component {
  render() {
    return (
      <OptimizelyProvider
        optimizely={optimizely}
        user={
          {
            id: 'user123',
            attributes: {
              'device': 'iPhone',
              'lifetime': 24738388,
              'is_logged_in': True,
            }
          }
        }>
        <App />
      </OptimizelyProvider>
    )
  }

Source files

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