React SDK
Segment membership
This package only evaluates published targeting lists. It has no membership write API and must not hold a Backend application key.
Update members from your backend. From Node, use the Node.js SDK. Flow.
Frontend telemetry
@ops-ai/react-feature-flags-toggly 1.12.0 adds frontend telemetry. Configure
enableTelemetry, metricsBaseUrl, and telemetryFlushIntervalMs on
new Toggly(...) or createTogglyProvider(...).
import { Toggly } from '@ops-ai/react-feature-flags-toggly'
// Standalone browser owner: invoke this example for a completed checkout.
const toggly = new Toggly({
appKey: 'your-app-key',
environment: 'Production',
metricsBaseUrl: 'https://metrics.toggly.io',
})
toggly.recordUsage('checkout')
toggly.recordView('checkout', 'control')
toggly.incrementCounter('orders')
toggly.setGauge('cartValue', 29.95)
await toggly.flushTelemetry()
toggly.dispose()
A provider factory shares one owner across its mounted instances and disposes after final unmount. Dispose directly owned services yourself. See the frontend telemetry contract.
Use matching React and React DOM versions: React 18.2+ or React 19.x
(^18.2.0 || ^19.0.0). The SDK shares your application's React and JSX runtimes
through peer dependencies. Existing React 18 applications can keep their
React 18 setup. See the support requirements for
the peer range.
Start with the React SDK sample. Read its README, then work through its home, gates, programmatic API, identity, Order context, and filter examples in order. Use the Samples catalog to find the other runnable SDK examples.
Use Toggly's React SDK in React applications.
Session identity
Supply instanceId at initialization only after a trusted backend mints it.
For the initialized service, await setContext({ identity: 'user-123', instanceId: 'minted-token' })
to change identity and token together. Clear both at logout with
setContext({ identity: '', instanceId: '', groups: [], claims: {} }).
Omitted groups and claims retain their values; explicitly empty groups and
claims remove old targeting. Supplying identity without instanceId clears the
prior token. Omitting both identity and instanceId retains the token.
| Context update | Existing instanceId |
|---|---|
setContext({ identity: 'next-user' }) | Cleared |
setContext({ groups: [] }) | Retained |
setContext({ instanceId: '' }) | Cleared |
See session attribution.
Installation
Install the React feature flags package using NPM:
$ npm i -s @ops-ai/react-feature-flags-toggly
React and React DOM are installed by the host application. Keep their versions matched; for a new React 19 application:
npm install react@19 react-dom@19 @ops-ai/react-feature-flags-toggly
npm install --save-dev @types/react@19 @types/react-dom@19
For an existing React 18 application, retain React/React DOM 18.2 or later in the 18.x line and matching React 18 type packages. The provider and component setup below is the same for both supported majors.
Basic Usage (with Toggly.io)
Setup Provider
Import createTogglyProvider in your index file:
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { createTogglyProvider } from '@ops-ai/react-feature-flags-toggly'
Create a TogglyProvider with your App Key & Environment name from your Toggly application page:
const TogglyProvider = await createTogglyProvider({
appKey: 'your-app-key', // You can find this in Toggly.io
environment: 'your-environment-name', // You can find this in Toggly.io
onError: (message, error) => {
// Report fetch/cache/refresh failures to your monitoring provider
console.warn('Toggly error:', message, error)
},
})
Wrap your App component with the newly created TogglyProvider:
const root = createRoot(
document.getElementById('root') as HTMLElement,
)
root.render(
<StrictMode>
<TogglyProvider>
<App />
</TogglyProvider>
</StrictMode>,
)
Using the Feature Component
Now you can start using the Feature component anywhere in your application by importing it:
import { Feature } from '@ops-ai/react-feature-flags-toggly'
<Feature featureKey={'firstFeature'}>
<p>This feature can be turned on or off.</p>
</Feature>
Feature Component Options
You can also check multiple feature keys and make use of the requirement (all/any) and negate (bool) options (requirement is set to "all" by default).
Show if all features are on
<Feature featureKeys={['firstFeature', 'secondFeature']}>
<p>ALL the provided feature keys are TRUE.</p>
</Feature>
Show if any feature is on
<Feature featureKeys={['firstFeature', 'secondFeature']} requirement={'any'}>
<p>AT LEAST ONE the provided feature keys is TRUE.</p>
</Feature>
Show if features are off (negate)
<Feature featureKeys={['firstFeature', 'secondFeature']} negate={true}>
<p>NONE of the provided feature keys is TRUE.</p>
</Feature>
Feature vs conditional UI
| Need | Use |
|---|---|
| Remove content from the DOM when off | <Feature> children, or a second <Feature negate> |
| Keep content mounted; drive CSS, disabled state, or handlers | <Feature render={...} />, useFeatureFlag, or useFeatureGate |
Use <Feature> when you only need show/hide. When you need styling, taps, or behavior driven by the resolved gate boolean (while keeping content mounted), use the render prop or hooks:
import { Feature, useFeatureGate } from '@ops-ai/react-feature-flags-toggly'
<Feature
featureKey="PremiumCheckout"
render={(enabled) => (
<button className={enabled ? 'active' : ''} disabled={!enabled}>
Checkout
</button>
)}
/>
function CheckoutPanel() {
const { isEnabled, isLoading } = useFeatureGate(['PremiumCheckout'])
if (isLoading) return null
return <section className={isEnabled ? 'promoted' : 'muted'}>...</section>
}
Both paths subscribe to remote flag refreshes and notifyLocalGatesChanged() for device-local post-filter gates.
Users and Rollouts
Using this package with Toggly allows you to define custom feature rollouts.
Custom rollouts offers the ability to show features only to certain groups of users based on various custom rules which you can define in Toggly.
In case you want to support custom feature rollouts, remember to provide an unique identity string for each user to make sure they get the same feature values on future visits:
const TogglyProvider = await createTogglyProvider({
appKey: 'your-app-key', // You can find this in Toggly.io
environment: 'your-environment-name', // You can find this in Toggly.io
identity: 'unique-user-identifier', // Use this in case you want to support custom feature rollouts
})
For User Claims and group targeting, call setContext() on the service after login:
import { useContext } from 'react';
import { context } from '@ops-ai/react-feature-flags-toggly';
// Inside a component rendered under the provider:
const { toggly } = useContext(context);
await toggly?.setContext({
identity: user.id,
groups: user.groups,
claims: { role: user.role, plan: user.plan },
});
See Feature filters and Evaluated-signed.
When using user identifiers, evaluated features are cached per user for 30 minutes by default on a sliding window, so the user might not see the change right away as to not confuse the user. The session length is configurable and the session store can be cleared in App Settings
Error reporting and refresh behavior
Pass onError when creating the provider to observe fetch, cache, parse, and
refresh failures:
const TogglyProvider = await createTogglyProvider({
appKey: 'your-app-key',
environment: 'your-environment-name',
onError: (message, error) => {
monitoring.captureException(error, {
tags: { source: 'toggly' },
extra: { message }
});
},
});
The service also exposes lastError, which is updated before onError runs.
After one successful load, refresh failures keep the last-known-good flags
instead of clearing rendered content. Feature, hooks, and provider state
subscribe to flag refreshes, so interval and WebSocket updates can re-render
React UI automatically.
For the shared reliability contract, see Reliability and Error Handling.
Basic Usage (without Toggly.io)
You can also use the React SDK without connecting to Toggly.io by providing feature defaults:
Setup Provider with Defaults
Import createTogglyProvider in your index file:
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { createTogglyProvider } from '@ops-ai/react-feature-flags-toggly'
Create a TogglyProvider and provide your feature defaults:
const featureDefaults = {
mainDescription: true,
documentationItem: true,
toolingItem: true,
}
const TogglyProvider = await createTogglyProvider({
featureDefaults: featureDefaults
})
Wrap your App component with the newly created TogglyProvider:
const root = createRoot(
document.getElementById('root') as HTMLElement,
)
root.render(
<StrictMode>
<TogglyProvider>
<App />
</TogglyProvider>
</StrictMode>,
)
Now you can use the Feature component the same way as with Toggly.io:
<Feature featureKey={'firstFeature'}>
<p>This feature can be turned on or off.</p>
</Feature>
TypeScript Support
The React SDK includes full TypeScript support with type definitions:
import { Feature, createTogglyProvider } from '@ops-ai/react-feature-flags-toggly'
interface FeatureProps {
featureKey?: string
featureKeys?: string[]
requirement?: 'all' | 'any'
negate?: boolean
}
Best Practices
- Initialize Provider Once: Create the TogglyProvider at the root of your app
- Use Feature Component: Prefer using the Feature component for declarative feature flag checks
- Provide User Context: Include identity for accurate targeting and rollouts
- Set Feature Defaults: Provide defaults for offline scenarios or when not using Toggly.io
- TypeScript: Take advantage of TypeScript support for better type safety