UNPKG

@featurevisor/react

Version:

React package for Featurevisor

21 lines 749 B
import { useEffect, useState } from "react"; import { useSdk } from "./useSdk"; import { onFeatureChange } from "./onFeatureChange"; export function useFlag(featureKey, context = {}) { const sdk = useSdk(); const initialValue = sdk.isEnabled(featureKey, context); const [isEnabled, setIsEnabled] = useState(initialValue); useEffect(() => { const unsubscribe = onFeatureChange(sdk, featureKey, () => { const newValue = sdk.isEnabled(featureKey, context); if (newValue !== isEnabled) { setIsEnabled(newValue); } }); return () => { unsubscribe(); }; }, [featureKey, context]); return isEnabled; } //# sourceMappingURL=useFlag.js.map