@scaleway/use-growthbook 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @scaleway/use-growthbook
2
2
 
3
+ ## 1.0.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1572](https://github.com/scaleway/scaleway-lib/pull/1572) [`57ddd1a`](https://github.com/scaleway/scaleway-lib/commit/57ddd1a39c048675f343c0da8735714f1cb5678a) Thanks [@philibea](https://github.com/philibea)! - when attributes a of provider change there is new instance of growthbook created and can make a desync during render
8
+
9
+ - [#1566](https://github.com/scaleway/scaleway-lib/pull/1566) [`2223dc1`](https://github.com/scaleway/scaleway-lib/commit/2223dc1ec4993b57e4dbeb41f65a8659c63d548c) Thanks [@renovate](https://github.com/apps/renovate)! - Updated dependency `@growthbook/growthbook-react` to `0.19.0`.
10
+
11
+ ## 1.0.3
12
+
13
+ ### Patch Changes
14
+
15
+ - [#1518](https://github.com/scaleway/scaleway-lib/pull/1518) [`ca2aa68`](https://github.com/scaleway/scaleway-lib/commit/ca2aa68fcde0470dde378b4322ff55e9caec170f) Thanks [@Slashgear](https://github.com/Slashgear)! - Export directly some usable type on provider init attributes
16
+
3
17
  ## 1.0.2
4
18
 
5
19
  ### Patch Changes
@@ -2,28 +2,31 @@ import { GrowthBookProvider, GrowthBook } from '@growthbook/growthbook-react';
2
2
  import { useMemo, useCallback, useEffect } from 'react';
3
3
  import { jsx } from 'react/jsx-runtime';
4
4
 
5
+ const defaultLoadConfig = {
6
+ autoRefresh: false,
7
+ timeout: 500,
8
+ skipCache: false
9
+ };
5
10
  const getGrowthBookInstance = _ref => {
6
11
  let {
7
12
  config: {
8
13
  apiHost,
9
14
  clientKey,
10
- enableDevMode
15
+ enableDevMode,
16
+ backgroundSync,
17
+ subscribeToChanges
11
18
  },
12
- attributes,
13
19
  trackingCallback
14
20
  } = _ref;
15
21
  return new GrowthBook({
16
22
  apiHost,
17
23
  clientKey,
18
24
  enableDevMode,
19
- attributes,
20
- trackingCallback
25
+ trackingCallback,
26
+ backgroundSync,
27
+ subscribeToChanges
21
28
  });
22
29
  };
23
- const defaultLoadConfig = {
24
- autoRefresh: false,
25
- timeout: 500
26
- };
27
30
  const AbTestProvider = _ref2 => {
28
31
  let {
29
32
  children,
@@ -31,21 +34,29 @@ const AbTestProvider = _ref2 => {
31
34
  trackingCallback,
32
35
  errorCallback,
33
36
  attributes,
34
- loadConfig = defaultLoadConfig
37
+ loadConfig
35
38
  } = _ref2;
36
39
  const growthbook = useMemo(() => getGrowthBookInstance({
37
40
  config,
38
- attributes,
39
41
  trackingCallback
40
- }), [trackingCallback, config, attributes]);
42
+ }), [trackingCallback, config]);
41
43
  const loadFeature = useCallback(async () => {
42
44
  if (config.clientKey) {
43
- await growthbook.loadFeatures(loadConfig);
45
+ await growthbook.loadFeatures(loadConfig ?? defaultLoadConfig);
44
46
  }
45
47
  }, [growthbook, config, loadConfig]);
46
48
  useEffect(() => {
47
49
  loadFeature().catch(errorCallback);
48
50
  }, [loadFeature, errorCallback]);
51
+ useEffect(() => {
52
+ const currentAttributes = growthbook.getAttributes();
53
+ if (currentAttributes !== attributes) {
54
+ growthbook.setAttributes({
55
+ ...currentAttributes,
56
+ ...attributes
57
+ });
58
+ }
59
+ }, [attributes, growthbook]);
49
60
  return jsx(GrowthBookProvider, {
50
61
  growthbook: growthbook,
51
62
  children: children
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Context } from '@growthbook/growthbook-react';
2
- export { Context, FeatureString, FeaturesReady, IfFeatureEnabled, useExperiment, useFeature, useFeatureIsOn, useFeatureValue, withRunExperiment } from '@growthbook/growthbook-react';
2
+ export { FeatureString, FeaturesReady, IfFeatureEnabled, useExperiment, useFeature, useFeatureIsOn, useFeatureValue, useGrowthBook, useGrowthBookSSR, withRunExperiment } from '@growthbook/growthbook-react';
3
3
  import * as react from 'react';
4
4
  import { ReactNode } from 'react';
5
5
 
@@ -7,28 +7,27 @@ type Attributes = Record<string, string | number | undefined>;
7
7
  /**
8
8
  * @param {boolean} [autoRefresh] - false.
9
9
  * @param {number} [timeout] - 500.
10
+ * @param {boolean} [skipCache] - false.
10
11
  */
11
12
  type LoadConfig = {
12
13
  autoRefresh: boolean;
13
14
  timeout: number;
15
+ skipCache: boolean;
14
16
  };
17
+ type ToolConfig = Pick<Context, 'apiHost' | 'clientKey' | 'enableDevMode' | 'backgroundSync' | 'subscribeToChanges'>;
18
+ type TrackingCallback = NonNullable<Context['trackingCallback']>;
19
+ type ErrorCallback = (error: Error | string) => void | null;
15
20
 
16
21
  declare const useAbTestAttributes: () => [Attributes, (attributes: Attributes) => void];
17
22
 
18
- type ToolConfig = {
19
- apiHost: string;
20
- clientKey: string;
21
- enableDevMode: boolean;
22
- };
23
- type TrackingCallback = NonNullable<Context['trackingCallback']>;
24
23
  type AbTestProviderProps = {
25
24
  children: ReactNode;
26
25
  config: ToolConfig;
27
26
  trackingCallback: TrackingCallback;
28
- errorCallback: (error: Error | string) => void;
27
+ errorCallback: ErrorCallback;
29
28
  attributes: Attributes;
30
29
  loadConfig?: LoadConfig;
31
30
  };
32
31
  declare const AbTestProvider: ({ children, config, trackingCallback, errorCallback, attributes, loadConfig, }: AbTestProviderProps) => react.JSX.Element;
33
32
 
34
- export { AbTestProvider, useAbTestAttributes };
33
+ export { AbTestProvider, type ErrorCallback, type ToolConfig, type TrackingCallback, useAbTestAttributes };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export { useExperiment, useFeature, useFeatureIsOn, useFeatureValue, withRunExperiment } from '@growthbook/growthbook-react';
1
+ export { FeatureString, FeaturesReady, IfFeatureEnabled, useExperiment, useFeature, useFeatureIsOn, useFeatureValue, useGrowthBook, useGrowthBookSSR, withRunExperiment } from '@growthbook/growthbook-react';
2
2
  export { useAbTestAttributes } from './useAbTestAttributes.js';
3
3
  export { AbTestProvider } from './AbTestProvider.js';
@@ -1,14 +1,16 @@
1
1
  import { useGrowthBook } from '@growthbook/growthbook-react';
2
- import { useMemo, useCallback } from 'react';
3
2
 
4
3
  const useAbTestAttributes = () => {
5
4
  const growthBook = useGrowthBook();
6
- const attributes = useMemo(() => growthBook?.getAttributes() ?? {}, [growthBook]);
7
- const setAttributes = useCallback(newAttributes => growthBook?.setAttributes({
8
- ...attributes,
9
- ...newAttributes
10
- }), [growthBook, attributes]);
11
- return [attributes, setAttributes];
5
+ if (growthBook) {
6
+ const attributes = growthBook.getAttributes();
7
+ const setAttributes = newAttributes => growthBook.setAttributes({
8
+ ...attributes,
9
+ ...newAttributes
10
+ });
11
+ return [attributes, setAttributes];
12
+ }
13
+ return [{}, () => {}];
12
14
  };
13
15
 
14
16
  export { useAbTestAttributes };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scaleway/use-growthbook",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Utility package to expose AB test tool",
5
5
  "type": "module",
6
6
  "engines": {
@@ -25,7 +25,7 @@
25
25
  "feature flags"
26
26
  ],
27
27
  "dependencies": {
28
- "@growthbook/growthbook-react": "0.18.0"
28
+ "@growthbook/growthbook-react": "0.19.0"
29
29
  },
30
30
  "devDependencies": {
31
31
  "react": "18.2.0"