configcat-react
Version:
ConfigCat is a configuration as a service that lets you manage your features and configurations without actually deploying new code.
30 lines (29 loc) • 1.51 kB
JavaScript
"use client";
import { useContext, useEffect, useState } from "react";
import { getConfigCatContext } from "./ConfigCatContext";
import { createConfigCatProviderError } from "./ConfigCatProvider";
function useFeatureFlag(key, defaultValue, user, providerId) {
var configCatContextObj = getConfigCatContext(providerId);
if (!configCatContextObj)
throw createConfigCatProviderError("useFeatureFlag", providerId);
var configCatContext = useContext(configCatContextObj);
if (!configCatContext)
throw createConfigCatProviderError("useFeatureFlag", providerId);
var _a = useState(defaultValue), featureFlagValue = _a[0], setFeatureFlag = _a[1];
var _b = useState(true), loading = _b[0], setLoading = _b[1];
useEffect(function () {
configCatContext.client.getValueAsync(key, defaultValue, user)
.then(function (v) { setFeatureFlag(v); setLoading(false); });
}, [configCatContext, key, defaultValue, JSON.stringify(user)]);
return { value: featureFlagValue, loading: loading };
}
function useConfigCatClient(providerId) {
var configCatContextObj = getConfigCatContext(providerId);
if (!configCatContextObj)
throw createConfigCatProviderError("useConfigCatClient", providerId);
var configCatContext = useContext(configCatContextObj);
if (!configCatContext)
throw createConfigCatProviderError("useConfigCatClient", providerId);
return configCatContext.client;
}
export { useFeatureFlag, useConfigCatClient };