React SDK
Integrate FlagSync feature flags into React applications using hooks and providers.
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.
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 with FlagSyncProvider
and provide the required configuration:
Ensure the key
in FsUserContext
is unique and persistent for accurate MAU tracking and consistent flag evaluations. See User Context Best Practices for details.
Wait for Readiness
Prevent flickering by ensuring the SDK is ready before your app renders.
Use the waitForReady
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.
Using hooks or render prop components
Check isReady with hooks like useFlag
, useFlags
, or useFlagSyncClient
to control rendering
Identical property are provided in <FlagSyncFlag />
, and <FlagSyncFlags />
.
Usage
Evaluate Flags
Evaluate flags using either hooks or render prop components. Both methods apply targeting rules, rollouts, and defaults for the user context:
- An Impression is automatically logged either hooks or render props are called.
- Memoization prevents duplicate impressions from external renders.
Hooks
Render Props
SDK Not Ready
If the SDK isn’t ready, it returns the defaultValue
or control
:
SDK Ready
Once ready, hooks and render props returns the server-evaluated value:
- Client-side SDKs can bootstrap via
LocalStorage
or an initial flag set—values apply until the SDK is ready. - See Flag Evaluation: Overview.
Track Events
Submit user actions with the useTrack
hook, which supports two usage patterns:
With a pre-filled event key
Bind the hook to a specific event key for repeated tracking:
With a dynamic event key
Use a generic track function to specify the event key at call time:
- See JavaScript SDK: Track Events for more on
track()
. - See Events: Tracking to learn about numeric and property events.
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
Access these events using the useFlagSyncClient
hook:
SDK_UPDATE
does not fire if syncing is disabled.
Configuration
Configure the SDK with the FsConfig
interface:
Custom Attributes
Define user attributes for targeting in Flags: User Segments.
Ensure the key
in FsUserContext
is unique and persistent for accurate MAU tracking and consistent flag evaluations. See User Context Best Practices for details.
Flag Syncing
Configure flag update strategies with the sync
object: stream
, poll
, or off
.
By default, flag updates propagate in milliseconds via server-side events (SSE), ensuring the latest values are used in evaluations.
- Components using flag hooks or render props re-render automatically—no event listeners needed.
- To disable this behavior, set
sync
tooff
, requiring a page refresh to fetch updated flags.
Stream (Default)
Stream updates via SSE—flag updates are reevaluated on the server and sent to the client:
Polling
Poll the server on an interval:
Off
Disable syncing:
Bootstrapping
Initialize the SDK with a set of bootstrap flags:
Bootstrapped values apply before the SDK is ready—see Flag Evaluation: Overview.
Storage
Choose between memory and LocalStorage storage types:
Memory (Default)
LocalStorage
Use the useFlagSyncClient
to get the client.isReadyFromStore
property.
Best Practices
- Use
isReady
checks to handle loading states. See Wait for Readiness. - Select a sync strategy (
stream
/poll
/off
) based on your application’s needs. - Use
useTrack
for 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)