Skip to content

Commit

Permalink
Added useFeatureFlagWithPayload for react native (#111)
Browse files Browse the repository at this point in the history
Co-authored-by: Manoel Aranda Neto <[email protected]>
  • Loading branch information
Jonatthu and marandaneto authored Oct 6, 2023
1 parent 73d2fc2 commit dcd429a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions posthog-react-native/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Next

1. Added new `const [flag, payload] = useFeatureFlagWithPayload('my-flag-name')` hook that returns the flag result and it's payload if it has one.

# 2.7.1 - 2023-05-25

1. The `$screen_name` property will be registered for all events whenever `screen` is called
Expand Down
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 dcd429a

Please sign in to comment.