There are two ways to implement feature flags in React Native: 1. Using hooks. 2. Loading the flag directly. ### Method 1: Using hooks #### Example 1: Boolean feature flags ```react-native import { useFeatureFlag } from 'posthog-react-native' const MyComponent = () => { const booleanFlag = useFeatureFlag('key-for-your-boolean-flag') if (booleanFlag === undefined) { // the response is undefined if the flags are being loaded return null } // Optional use the 'useFeatureFlagWithPayload' hook for fetching the feature flag payload return booleanFlag ? Testing feature 😄 : Not Testing feature 😢 } ``` #### Example 2: Multivariate feature flags ```react-native import { useFeatureFlag } from 'posthog-react-native' const MyComponent = () => { const multiVariantFeature = useFeatureFlag('key-for-your-multivariate-flag') if (multiVariantFeature === undefined) { // the response is undefined if the flags are being loaded return null } else if (multiVariantFeature === 'variant-name') { // replace 'variant-name' with the name of your variant // Do something } // Optional use the 'useFeatureFlagWithPayload' hook for fetching the feature flag payload return
} ``` ### Method 2: Loading the flag directly ```react-native // Defaults to undefined if not loaded yet or if there was a problem loading posthog.isFeatureEnabled('key-for-your-boolean-flag') // Defaults to undefined if not loaded yet or if there was a problem loading posthog.getFeatureFlag('key-for-your-boolean-flag') // Multivariant feature flags are returned as a string posthog.getFeatureFlag('key-for-your-multivariate-flag') // Optional fetch the payload returns 'JsonType' or undefined if not loaded yet or if there was a problem loading posthog.getFeatureFlagPayload('key-for-your-multivariate-flag') ``` ### Reloading flags PostHog loads feature flags when instantiated and refreshes whenever methods are called that affect the flag. If want to manually trigger a refresh, you can call `reloadFeatureFlagsAsync()`: ```react-native posthog.reloadFeatureFlagsAsync().then((refreshedFlags) => console.log(refreshedFlags)) ``` Or when you want to trigger the reload, but don't care about the result: ```react-native posthog.reloadFeatureFlags() ``` ### Feature flag caching The React Native SDK caches feature flag values in AsyncStorage. Cached values persist indefinitely with no TTL until updated by a successful API call. This enables offline support and reduces latency, but means **inactive users may see stale flag values** from their last session. For example, if a user last opened your app when a flag was `false`, that value remains cached even after you roll it out to 100%. When they reopen the app, the SDK returns the cached `false` first, then fetches the fresh `true` value from the API. To ensure fresh flag values: ```react-native // Force refresh on app start await posthog.reloadFeatureFlagsAsync() ``` Or clear cached values for inactive users: ```react-native if (lastActiveDate < migrationDate) { posthog.reset() // Clears all cached data } ``` ### Request timeout You can configure the `featureFlagsRequestTimeoutMs` parameter when initializing your PostHog client to set a flag request timeout. This helps prevent your code from being blocked in the case when PostHog's servers are too slow to respond. By default, this is set at 10 seconds. ```react-native export const posthog = new PostHog('', { // usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com' host: '', featureFlagsRequestTimeoutMs: 10000 // Time in milliseconds. Default is 10000 (10 seconds). }) ``` ### Error handling When using the PostHog SDK, it's important to handle potential errors that may occur during feature flag operations. Here's an example of how to wrap PostHog SDK methods in an error handler: ```react-native function handleFeatureFlag(client, flagKey, distinctId) { try { const isEnabled = client.isFeatureEnabled(flagKey, distinctId); console.log(`Feature flag '${flagKey}' for user '${distinctId}' is ${isEnabled ? 'enabled' : 'disabled'}`); return isEnabled; } catch (error) { console.error(`Error fetching feature flag '${flagKey}': ${error.message}`); // Optionally, you can return a default value or throw the error // return false; // Default to disabled throw error; } } // Usage example try { const flagEnabled = handleFeatureFlag(client, 'new-feature', 'user-123'); if (flagEnabled) { // Implement new feature logic } else { // Implement old feature logic } } catch (error) { // Handle the error at a higher level console.error('Feature flag check failed, using default behavior'); // Implement fallback logic } ``` ### Overriding server properties Sometimes, you might want to evaluate feature flags using properties that haven't been ingested yet, or were set incorrectly earlier. You can do so by setting properties the flag depends on with these calls: ```react-native posthog.setPersonPropertiesForFlags({'property1': 'value', property2: 'value2'}) ``` Note that these are set for the entire session. Successive calls are additive: all properties you set are combined together and sent for flag evaluation. Whenever you set these properties, we also trigger a reload of feature flags to ensure we have the latest values. You can disable this by passing in the optional parameter for reloading: ```react-native posthog.setPersonPropertiesForFlags({'property1': 'value', property2: 'value2'}, false) ``` At any point, you can reset these properties by calling `resetPersonPropertiesForFlags`: ```react-native posthog.resetPersonPropertiesForFlags() ``` The same holds for [group](/docs/product-analytics/group-analytics) properties: ```react-native // set properties for a group posthog.setGroupPropertiesForFlags({'company': {'property1': 'value', property2: 'value2'}}) // reset properties for all groups: posthog.resetGroupPropertiesForFlags() ``` > **Note:** You don't need to add the group names here, since these properties are automatically attached to the current group (set via `posthog.group()`). When you change the group, these properties are reset. **Automatic overrides** Whenever you call `posthog.identify` with person properties, we automatically add these properties to flag evaluation calls to help determine the correct flag values. The same is true for when you call `posthog.group()`. **Default overridden properties** By default, we always override some properties based on the user IP address. The list of properties that this overrides: 1. $geoip_city_name 2. $geoip_country_name 3. $geoip_country_code 4. $geoip_continent_name 5. $geoip_continent_code 6. $geoip_postal_code 7. $geoip_time_zone This enables any geolocation-based flags to work without manually setting these properties.