Overview
The@flagsync/react-sdk integrates into React applications for client-side feature management and event tracking—ideal for single-user contexts like browser environments.
Requires React 16.3+ due to its dependency on the React Context API.
npm
Latest version available on npm
GitHub
Explore the source code on GitHub
Installation
Install the SDK with your preferred package manager:Quickstart
A basic example of using the SDK in a React application:Initialization
Get Your SDK Key
Find your client-side SDK key in your workspace settings—safe for web/mobile apps (keep server-side keys private).Initialize the SDK
Wrap your application withFlagSyncProvider and provide the required configuration:
Wait for Readiness
Prevent flickering by ensuring the SDK is ready before your app renders. Use thewaitForReady prop in FlagSyncProvider to delay rendering until the SDK is initialized, or check readiness manually with hooks or render prop components:
Using waitForReady
Add
waitForReady to FlagSyncProvider to delay rendering until the SDK is ready, avoiding flicker during initialization.SDK Not Ready
If the SDK isn’t ready, it returns thedefaultValue or control:
SDK Ready
Once ready, hooks return the server-evaluated value:- Client-side SDKs can bootstrap via
LocalStorageor an initial flag set—values apply until the SDK is ready. - See Flag Evaluation: Overview.
Usage
Evaluate Flags
Evaluates a feature flag for user context. Applies targeting rules, rollouts, and defaults values.FlagSync CLI
When using the CLI, flag values are automatically inferred from the generated types:Track Events
Submit user actions with theuseTrack hook, which supports two usage patterns:
SDK Event Listeners
The SDK emits these events for SDK lifecycle management:SDK_UPDATE: Emitted when flags are updatedSDK_READY: Emitted when the SDK is readySDK_READY_FROM_STORE: Emitted when flags are loaded from storageERROR: Emitted when an error occurs during initialization
useFlagSyncClient hook:
SDK_UPDATE does not fire if syncing is disabled.Configuration
Configure the SDK with theFsConfig interface:
Custom Attributes
Define user attributes for targeting in Flags: User Segments.Flag Syncing
Configure flag update strategies with thesync object: ws, sse, poll, or off.
- Components using flag hooks re-render automatically—no event listeners needed.
- To disable this behavior, set
synctooff, requiring a page refresh to fetch updated flags.
WebSocket (Default)
Stream updates over a WebSocket connection—flag updates are reevaluated on the server and pushed to the client in real time:
Bootstrapping
Initialize the SDK with a set of bootstrap flags:Storage
Choose between memory and LocalStorage storage types:Best Practices
- Use
isReadychecks to handle loading states. See Wait for Readiness. - Select a sync strategy (
ws/sse/poll/off) based on your application’s needs. - Use
useTrackfor consistent event tracking in components. - Add user attributes for targeted feature rollouts.
- Consider bootstrapping for faster initial renders.
Environment Variables
Set the following environment variable:FLAGSYNC_SDK_KEY: Your client-side FlagSync SDK key (required)