Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI Reference
Dev guideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

withOptimizely for the React SDK

Describes the higher-order component withOptimizely.

By using the higher-order component (HoC) withOptimizely, any component under the OptimizelyProvider can access the Optimizely ReactSDKClient, which exposes Optimizely Feature Experimentation information and APIs.

Version

SDK v1.0.0

Description

The withOptimizely component provides access to all standard SDK methods, such as the Decide method, if you do not want to use React components or hooks (such as useDecision) to interact with Optimizely Feature Experimentation. By providing the wrapped component with an optimizely prop, the withOptimizely higher-order component also makes usage of the these standard SDK methods more convenient.

The optimizely object is automatically associated with the user prop passed to the ancestor OptimizelyProvider. The result is that the id and attributes from that user object will automatically be forwarded to all appropriate SDK method calls. So, there is no need to pass the userId or attributes arguments when calling methods of the optimizely client object after using withOptimizely, unless you wish to use a different userId or attributes than those given to OptimizelyProvider.

Parameters

This table lists the required and optional parameters for the React SDK.

ParameterTypeDescription
ComponentReact.ComponentComponent which will be enhanced with the following props

Props provided

PropTypeDescription
optimizelyReactSDKClientThe client object which was passed to the OptimizelyProvider
optimizelyReadyTimeoutnumber | undefinedThe timeout which was passed to the OptimizelyProvider
isServerSidebooleanValue that was passed to the OptimizelyProvider

Returns

A wrapped component with additional props as previously described.

Example

import { withOptimizely } from '@optimizely/react-sdk'

class MyComp extends React.Component {
  constructor(props) {
    super(props)
    const { optimizely } = this.props
    const decision = optimizely.decide('feat1')

    this.state = {
      decision.enabled,
      decision.variables,
    }
  }

  render() {
  }
}

const WrappedMyComponent = withOptimizely(MyComp)

Tracking (using withOptimizely)

Other Optimizely Feature Experimentation functionality that may be triggered by using this method.

import { withOptimizely } from '@optimizely/react-sdk'

class SignupButton extends React.Component {
  onClick = () => {
    const { optimizely } = this.props
    optimizely.onReady().then(() => {
      optimizely.track('signup-clicked')
    })
    // rest of click handler
  }

  render() {
    <button onClick={this.onClick}>
      Signup
    </button>
  }
}

const WrappedSignupButton = withOptimizely(SignupButton)

🚧

Important

As mentioned above, the optimizely client object provided through withOptimizely is automatically associated with the user prop passed to the ancestor OptimizelyProvider. There is no need to pass userId or attributes arguments when calling track, unless you wish to use different userId or attributes than those given to OptimizelyProvider.

Source

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