Claude Skills Guide

Claude Code PostHog Feature Flag React SDK Guide

Feature flags have become an essential part of modern software development, enabling teams to ship features incrementally, run A/B tests, and control feature rollouts without deploying new code. PostHog provides a powerful feature flag system, and when combined with React applications, it offers seamless user experience control. This guide shows you how to use Claude Code to work efficiently with PostHog feature flags in your React projects.

Setting Up PostHog in Your React Project

Before diving into Claude Code workflows, you need to configure PostHog in your React application. The PostHog React SDK provides a simple integration pattern. Start by installing the package:

npm install posthog-js
# or
yarn add posthog-js

Then initialize PostHog in your application entry point. Create a dedicated configuration file to keep your setup organized:

// lib/posthog.ts
import posthog from 'posthog-js'

posthog.init('YOUR_POSTHOG_API_KEY', {
  api_host: 'https://app.posthog.com',
  person_profiles: 'always',
  capture_pageview: true,
})

export default posthog

Claude Code can help you generate this setup automatically. Simply describe your requirements, and Claude will create the appropriate configuration files with best practices.

Creating Feature Flags in PostHog

Once your React app is connected to PostHog, you can create feature flags directly from the PostHog dashboard or programmatically through their API. Claude Code excels at interacting with APIs, making flag creation straightforward.

Here’s how to create a simple feature flag:

// Create a feature flag via PostHog API
const createFeatureFlag = async (name: string, key: string) => {
  const response = await fetch('https://app.posthog.com/api/feature_flag/', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.POSTHOG_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      name,
      key,
      filters: {
        groups: [{ properties: [], rollout_percentage: 100 }],
      },
    }),
  })
  return response.json()
}

With Claude Code, you can describe the flag you want to create, and it will generate the appropriate API call or dashboard configuration steps.

Using Feature Flags in React Components

The most common use case is checking feature flags within your React components to conditionally render UI. The PostHog React SDK provides a useFeatureFlag hook for this purpose:

import { useFeatureFlag } from 'posthog-js/react'

function NewDashboard() {
  const newDashboardEnabled = useFeatureFlag('new-dashboard-v2')
  
  if (newDashboardEnabled) {
    return <NewDashboardUI />
  }
  
  return <LegacyDashboardUI />
}

This pattern allows you to gradually roll out new features to users. You can target specific user segments by configuring flag rollouts in PostHog based on user properties.

Implementing Progressive Rollouts

One of the most powerful features of PostHog is the ability to implement progressive rollouts. Instead of a simple on/off toggle, you can gradually expose features to a percentage of users:

// Configure a gradual rollout in PostHog
const gradualRolloutConfig = {
  key: 'experimental-search',
  filters: {
    groups: [
      {
        properties: [],
        rollout_percentage: 10, // Start with 10%
      },
    ],
    multivariate: {
      variants: [
        { key: 'control', name: 'Control', rollout_percentage: 90 },
        { key: 'test', name: 'Test', rollout_percentage: 10 },
      ],
    },
  },
}

Claude Code can help you generate these configurations and even create scripts to automate the rollout process as you gain confidence in your feature.

Handling Edge Cases and Loading States

When working with feature flags in React, you need to handle various states properly. The flag might not be loaded immediately, or the user might be in a specific segment. Here’s a robust pattern:

import { useFeatureFlag, useFeatureFlagEnabled } from 'posthog-js/react'

function FeatureWrapper({ flagKey, children, fallback = null }) {
  const flagValue = useFeatureFlag(flagKey)
  
  // Handle loading state
  if (flagValue === undefined) {
    return <SkeletonLoader />
  }
  
  // Handle flag disabled
  if (!flagValue) {
    return fallback
  }
  
  return children
}

// Usage
<FeatureWrapper flagKey="new-checkout-flow" fallback={<LegacyCheckout />}>
  <NewCheckoutFlow />
</FeatureWrapper>

Testing Feature Flags

Testing is crucial when implementing feature flags. You need to ensure your application behaves correctly regardless of flag states. Claude Code can generate test cases for you:

// Test file example
import { render, screen } from '@testing-library/react'
import { PostHogProvider } from 'posthog-js/react'
import { NewFeature } from './NewFeature'

const renderWithFlag = (flagValue: boolean) => {
  // Mock the feature flag hook
  jest.spyOn(require('posthog-js/react'), 'useFeatureFlag')
    .mockReturnValue(flagValue)
  
  return render(
    <PostHogProvider client={{}}>
      <NewFeature />
    </PostHogProvider>
  )
}

test('shows new feature when flag is enabled', () => {
  renderWithFlag(true)
  expect(screen.getByText('New Feature Content')).toBeInTheDocument()
})

test('shows fallback when flag is disabled', () => {
  renderWithFlag(false)
  expect(screen.getByText('Legacy Content')).toBeInTheDocument()
})

Integrating with Claude Code Workflows

Claude Code can significantly accelerate your feature flag workflows. Here are some practical ways to use it:

1. Generate Flag Configurations Describe your desired rollout strategy, and Claude will create the appropriate PostHog API payload or dashboard configuration.

2. Create Flag Hooks Need a custom hook that wraps PostHog functionality with additional logic? Describe your requirements:

“Create a hook that checks multiple flags and returns a feature set object”

Claude will generate a custom hook like this:

import { useFeatureFlag } from 'posthog-js/react'

export function useFeatureSet(flags: string[]) {
  return flags.reduce((acc, flag) => {
    acc[flag] = useFeatureFlag(flag)
    return acc
  }, {} as Record<string, boolean>)
}

// Usage
const features = useFeatureSet(['new-dashboard', 'beta-search', 'dark-mode'])
if (features['new-dashboard']) {
  // render new dashboard
}

3. Automate Flag Cleanup Over time, feature flags accumulate. Claude can help you identify and remove stale flags from your codebase.

Best Practices

When implementing feature flags with PostHog in React, follow these best practices:

  1. Name flags consistently: Use a naming convention like feature-description or team-feature-name
  2. Remove dead code: Once a feature is fully rolled out, remove the flag check and legacy code
  3. Monitor performance: PostHog flags are evaluated locally, but track any latency impact
  4. Use local evaluation: Enable local evaluation for faster flag checks when possible
  5. Document flag purpose: Add comments explaining why each flag exists and when it will be removed

Conclusion

PostHog feature flags combined with React provide a powerful toolkit for progressive delivery and experimentation. Claude Code enhances this workflow by helping you generate configurations, create custom hooks, write tests, and manage your flag lifecycle. Start with simple on/off flags, then gradually adopt multivariate testing and gradual rollouts as your feature flag strategy matures.

The key is to keep your implementation clean, test thoroughly, and remove flags once features are established. With these patterns and Claude Code as your assistant, you’ll have a robust feature management system that enables safe, data-driven feature releases.

Built by theluckystrike — More at zovo.one