Skip to content

Instantly share code, notes, and snippets.

@jordangarcia
Created April 1, 2019 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordangarcia/1974cc7c08d64ae80ca3731db21f9056 to your computer and use it in GitHub Desktop.
Save jordangarcia/1974cc7c08d64ae80ca3731db21f9056 to your computer and use it in GitHub Desktop.

3. Instantiate the client

We recommend creating an optimizely.js that exports an instantiated Optimizely client. This file can be required anywhere you want to use feature flagging.

// optimizely.js
import optimizelySDK from '@optimizely/optimizely-sdk'

const optimizely = optimizelySDK.createInstance({
  datafile: window.optimizelyDatafile['RWsw743bm1s8FwFaVXihzX'],
})

export default optimizely

The optimizely instance can be required and used anywhere that is needed.

import optimizely from './optimizely'

const enabled = optimizely.isFeatureEnabled('new_feature', 'user123');
if (enabled) {
  // Show your feature
}

This is just one way to maintain reference to an Optimizely client. Other options include storing it as a global variable on window or putting it in a Redux store.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment