Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/puny-rules-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@posthog/react': patch
---

Updated feature flag hooks to properly check if client is initialized and prevent client is undefined errors
2 changes: 1 addition & 1 deletion packages/react/src/hooks/useActiveFeatureFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function useActiveFeatureFlags(): string[] {
}, [client])

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

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/useFeatureFlagEnabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function useFeatureFlagEnabled(flag: string): boolean | undefined {
const bootstrapped = bootstrap?.featureFlags?.[flag]

// if the client is not loaded yet, check if we have a bootstrapped value and then true/false it
if (!client.featureFlags.hasLoadedFlags && bootstrap?.featureFlags) {
if (!client?.featureFlags?.hasLoadedFlags && bootstrap?.featureFlags) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Other similar hooks (useFeatureFlagVariantKey.ts:17, useFeatureFlagPayload.ts:17, useActiveFeatureFlags.ts:16) don't use optional chaining but this one does - consider whether this change is actually needed or if the issue is elsewhere

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/react/src/hooks/useFeatureFlagEnabled.ts
Line: 19:19

Comment:
**style:** Other similar hooks (`useFeatureFlagVariantKey.ts:17`, `useFeatureFlagPayload.ts:17`, `useActiveFeatureFlags.ts:16`) don't use optional chaining but this one does - consider whether this change is actually needed or if the issue is elsewhere

How can I resolve this? If you propose a fix, please make it concise.

return isUndefined(bootstrapped) ? undefined : !!bootstrapped
}

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/useFeatureFlagPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function useFeatureFlagPayload(flag: string): JsonType {
}, [client, flag])

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

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/useFeatureFlagVariantKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function useFeatureFlagVariantKey(flag: string): string | boolean | undef
})
}, [client, flag])

if (!client.featureFlags.hasLoadedFlags && bootstrap?.featureFlags) {
if (!client?.featureFlags?.hasLoadedFlags && bootstrap?.featureFlags) {
return bootstrap.featureFlags[flag]
}

Expand Down
Loading