← Back to API Tester

@rollgate/react SDK

Feature flags for React applications with real-time updates via SSE

Installation

npm install @rollgate/react

1. Setup Provider

Wrap your app with RollgateProvider. Use mode: 'sse' for real-time updates.

tsx
// App.tsx import { RollgateProvider } from '@rollgate/react'; function App() { return ( <RollgateProvider apiKey="rg_client_xxxxx" apiUrl="https://api.rollgate.io" mode="sse" // Real-time updates via Server-Sent Events > <YourApp /> </RollgateProvider> ); }

2. Use Feature Flags

Use the useFlag hook to check if a feature is enabled.

tsx
// MyComponent.tsx import { useFlag } from '@rollgate/react'; function MyComponent() { const darkMode = useFlag('dark-mode'); return ( <div className={darkMode ? 'dark' : 'light'}> Dark mode is {darkMode ? 'ON' : 'OFF'} </div> ); }

3. User Targeting (Optional)

Identify users to enable percentage rollouts and targeting rules.

tsx
import { useRollgate } from '@rollgate/react'; function UserProfile({ user }) { const { identify } = useRollgate(); useEffect(() => { identify({ userId: user.id, attributes: { plan: user.plan, // 'free' | 'pro' | 'enterprise' country: user.country // For geo-targeting } }); }, [user]); return <div>...</div>; }

Features

Real-time Updates
SSE mode pushes flag changes instantly
🎯
User Targeting
Target by user ID, attributes, or percentage
🔄
Polling Fallback
Falls back to polling if SSE unavailable
📦
Lightweight
~3KB gzipped, zero dependencies

Links