Skip to content

Instantly share code, notes, and snippets.

@frehner
Last active June 18, 2020 21:20
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 frehner/c6fa439b458e5151dcaa0e07e174a038 to your computer and use it in GitHub Desktop.
Save frehner/c6fa439b458e5151dcaa0e07e174a038 to your computer and use it in GitHub Desktop.
import React from 'react'
import {
OptimizelyFeature as OriginalOptimizelyFeature,
useFeature as useFeatureOriginal,
} from '@optimizely/react-sdk'
export function OptimizelyFeature({ feature, children, ...restProps }) {
return (
<OriginalOptimizelyFeature feature={feature} {...restProps}>
{(isEnabled, ...rest) => {
let isEnabledFinal = isEnabled
const storageSetting = localStorage.getItem(feature)
if (storageSetting) {
// allows the local override to always win out, even if the env has the toggle on
isEnabledFinal = storageSetting === 'true'
}
return children(isEnabledFinal, ...rest)
}}
</OriginalOptimizelyFeature>
)
}
export function useFeature(...data) {
const [isEnabled, ...rest] = useFeatureOriginal(...data)
let isEnabledFinal = isEnabled
const storageSetting = localStorage.getItem(data[0])
if (storageSetting) {
// allows the local override to always win out, even if the env has the toggle on
isEnabledFinal = storageSetting === 'true'
}
return [isEnabledFinal, ...rest]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment