FlagsProvider
Overview
Section titled “Overview”FlagsProvider is a React context provider that initializes the Flags.gg client and makes feature flags available to all child components via the useFlags hook.
import { FlagsProvider } from "@flags-gg/react-library";
function App() { return ( <FlagsProvider options={{ projectId: "YOUR_PROJECT_ID", agentId: "YOUR_AGENT_ID", environmentId: "YOUR_ENVIRONMENT_ID", }}> <YourApp /> </FlagsProvider> );}options (required)
Section titled “options (required)”| Property | Type | Required | Description |
|---|---|---|---|
projectId |
string |
Yes | Your Flags.gg project ID |
agentId |
string |
Yes | Your agent ID |
environmentId |
string |
Yes | Your environment ID |
flagsURL |
string |
No | Custom API endpoint (defaults to https://api.flags.gg/flags) |
enableLogs |
boolean |
No | Enable debug logging |
targetingKey |
string |
No | Stable user id for percentage rollouts. Defaults to a random id stored in localStorage |
context |
object |
No | User attributes for targeting rules, e.g. { email, plan, groups } |
Rollouts and targeting rules
Section titled “Rollouts and targeting rules”<FlagsProvider options={{ projectId, agentId, environmentId, targetingKey: user.id, context: { email: user.email, plan: user.plan, groups: user.groups } }}>is("flag").enabled() already accounts for both. If storage is blocked and there’s no targetingKey, partially rolled-out flags stay off. See Rollouts & Targeting Rules.
How It Works
Section titled “How It Works”- On mount, the provider fetches flags from the API using the provided credentials
- Flags are cached in memory for the lifetime of the component
- The provider sets up a refresh interval based on the
intervalAllowedvalue from the API response (default: 15 minutes) - Local overrides are persisted in
localStorageunder the keyflagsgg:overrides - An abort controller handles cleanup on unmount
Placement
Section titled “Placement”Place FlagsProvider as high in your component tree as needed. It only needs to wrap the components that use useFlags:
// Wrap the entire app<FlagsProvider options={config}> <App /></FlagsProvider>
// Or wrap a specific section<FlagsProvider options={config}> <FeatureSection /></FlagsProvider>With React Router
Section titled “With React Router”import { BrowserRouter } from "react-router-dom";import { FlagsProvider } from "@flags-gg/react-library";
function App() { return ( <BrowserRouter> <FlagsProvider options={{ projectId: "YOUR_PROJECT_ID", agentId: "YOUR_AGENT_ID", environmentId: "YOUR_ENVIRONMENT_ID", }}> <Routes /> </FlagsProvider> </BrowserRouter> );}