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.