Skip to content

Commit d10783f

Browse files
fix: guard against undefined client in useFeatureFlagEnabled (#2655)
* guard against undefined client * changeset * Update .changeset/puny-rules-hide.md Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * add guard to other hooks --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent 4487d6b commit d10783f

File tree

5 files changed

+9
-4
lines changed

5 files changed

+9
-4
lines changed

.changeset/puny-rules-hide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@posthog/react': patch
3+
---
4+
5+
Updated feature flag hooks to properly check if client is initialized and prevent client is undefined errors

packages/react/src/hooks/useActiveFeatureFlags.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function useActiveFeatureFlags(): string[] {
1313
}, [client])
1414

1515
// if the client is not loaded yet and we have a bootstrapped value, use it
16-
if (!client.featureFlags.hasLoadedFlags && bootstrap?.featureFlags) {
16+
if (!client?.featureFlags?.hasLoadedFlags && bootstrap?.featureFlags) {
1717
return Object.keys(bootstrap.featureFlags)
1818
}
1919

packages/react/src/hooks/useFeatureFlagEnabled.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function useFeatureFlagEnabled(flag: string): boolean | undefined {
1616
const bootstrapped = bootstrap?.featureFlags?.[flag]
1717

1818
// if the client is not loaded yet, check if we have a bootstrapped value and then true/false it
19-
if (!client.featureFlags.hasLoadedFlags && bootstrap?.featureFlags) {
19+
if (!client?.featureFlags?.hasLoadedFlags && bootstrap?.featureFlags) {
2020
return isUndefined(bootstrapped) ? undefined : !!bootstrapped
2121
}
2222

packages/react/src/hooks/useFeatureFlagPayload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function useFeatureFlagPayload(flag: string): JsonType {
1414
}, [client, flag])
1515

1616
// if the client is not loaded yet, use the bootstrapped value
17-
if (!client.featureFlags.hasLoadedFlags && bootstrap?.featureFlagPayloads) {
17+
if (!client?.featureFlags?.hasLoadedFlags && bootstrap?.featureFlagPayloads) {
1818
return bootstrap.featureFlagPayloads[flag]
1919
}
2020

packages/react/src/hooks/useFeatureFlagVariantKey.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function useFeatureFlagVariantKey(flag: string): string | boolean | undef
1414
})
1515
}, [client, flag])
1616

17-
if (!client.featureFlags.hasLoadedFlags && bootstrap?.featureFlags) {
17+
if (!client?.featureFlags?.hasLoadedFlags && bootstrap?.featureFlags) {
1818
return bootstrap.featureFlags[flag]
1919
}
2020

0 commit comments

Comments
 (0)