@scaleway/use-growthbook
Version:
Utility package to expose AB test tool
55 lines (54 loc) • 1.43 kB
JavaScript
import { jsx } from "react/jsx-runtime";
import { GrowthBook, GrowthBookProvider } from "@growthbook/growthbook-react";
import { useMemo, useCallback, useEffect } from "react";
const defaultLoadConfig = {
skipCache: false,
timeout: 500
};
const getGrowthBookInstance = ({
config: { apiHost, clientKey, enableDevMode },
trackingCallback
}) => new GrowthBook({
apiHost,
clientKey,
enableDevMode,
trackingCallback
});
const AbTestProvider = ({
children,
config,
trackingCallback,
errorCallback,
attributes,
loadConfig
}) => {
const growthbook = useMemo(
() => getGrowthBookInstance({ config, trackingCallback }),
[trackingCallback, config]
);
const loadFeature = useCallback(async () => {
if (config.clientKey) {
const initConfig = {
...defaultLoadConfig,
...loadConfig
};
await growthbook.init(initConfig);
}
}, [growthbook, config, loadConfig]);
useEffect(() => {
loadFeature().catch(errorCallback);
}, [loadFeature, errorCallback]);
useEffect(() => {
const currentAttributes = growthbook.getAttributes();
if (currentAttributes !== attributes) {
growthbook.setAttributes({
...currentAttributes,
...attributes
}).catch(errorCallback);
}
}, [attributes, growthbook, errorCallback]);
return /* @__PURE__ */ jsx(GrowthBookProvider, { growthbook, children });
};
export {
AbTestProvider
};