Skip to content

Commit

Permalink
Update useFeatureFlag.ts
Browse files Browse the repository at this point in the history
Enabling feature flags with payload, this should be essential to have...
  • Loading branch information
Jonatthu committed Sep 27, 2023
1 parent 4a707fd commit 5daa793
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions posthog-react-native/src/hooks/useFeatureFlag.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useState } from 'react'
import { usePostHog } from './usePostHog'
import { JsonType } from 'posthog-core/src'

export function useFeatureFlag(flag: string): string | boolean | undefined {
const posthog = usePostHog()
Expand All @@ -18,3 +19,24 @@ export function useFeatureFlag(flag: string): string | boolean | undefined {

return featureFlag
}

export type FeatureFlagWithPayload = [boolean | string | undefined, JsonType | undefined];

export function useFeatureFlagWithPayload(flag: string): FeatureFlagWithPayload {
const posthog = usePostHog()

const [featureFlag, setFeatureFlag] = useState<FeatureFlagWithPayload>([undefined, undefined])

useEffect(() => {
if (!posthog) {
return
}

setFeatureFlag([posthog.getFeatureFlag(flag), posthog.getFeatureFlagPayload(flag)])
return posthog.onFeatureFlags(() => {
setFeatureFlag([posthog.getFeatureFlag(flag), posthog.getFeatureFlagPayload(flag)])
})
}, [posthog, flag])

return featureFlag;
}

0 comments on commit 5daa793

Please sign in to comment.