Skip to content

Instantly share code, notes, and snippets.

@prokizzle
Created May 19, 2020 04:09
Show Gist options
  • Save prokizzle/66725bdf96cb8d19b65fcafea1ac7f4d to your computer and use it in GitHub Desktop.
Save prokizzle/66725bdf96cb8d19b65fcafea1ac7f4d to your computer and use it in GitHub Desktop.
import React from 'react'
import PropTypes from 'prop-types'
const useFeatureFlag = ({ feature, Component }) => {
// Gives us a boolean we can use generally
const isEnabled = !!FeatureFlags[feature] && FeatureFlags[feature];
// Gives us a component we can wrap around child components to hide them if
// the feature is disabled
const FeatureFlag = ({ children }) =>
isEnabled ? (
<React.Fragment>{children}</React.Fragment>
) : (
<React.Fragment />
);
FeatureFlag.propTypes = {
children: PropTypes.arrayOf(PropTypes.element).isRequired
};
// Decorates a passed in component with featureEnabled so that
// we can add our own conditional logic to our components
const DecoratedComponent = props => (
<Component featureEnabled={isEnabled} {...props} />
);
return { featureEnabled: isEnabled, FeatureFlag, DecoratedComponent };
}
export default useFeatureFlag;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment