React SDK
The Unleash React SDK lets you evaluate feature flags in React applications. It connects to Unleash or Unleash Edge to fetch evaluated flags for a given Unleash context.
You can use this SDK with Unleash Enterprise or Unleash Open Source.
For an overview of how Unleash SDKs work, including offline behavior, feature compatibility across SDKs, and default refresh and metrics intervals, refer to the SDK overview.
Installation
npm
yarn
Example application
To see the SDK in action, explore the examples/basic-app directory.
It contains a minimal Vite + React setup that connects to Unleash, evaluates a feature toggle,
and updates the evaluation context dynamically at runtime. The README in that folder includes step-by-step instructions
for running the example.
This library uses the core unleash-js-sdk client as a base.
Initialization
You can use the Frontend API or unleash-edge.
For the following step you will need a frontend API Token or unleash-edge pretrusted token.
Import the provider like this in your entrypoint file (typically index.js/ts):
Testing feature flags locally? The Developer Toolbar provides a drop-in replacement for FlagProvider that lets you override flag values and context at runtime. Use UnleashToolbarProvider from @unleash/toolbar/react during development.
Connection options
Set url to the /api/frontend endpoint of your Unleash instance or your Unleash Edge instance. Set clientKey to a frontend API token.
To connect this SDK to unleash-edge, follow the documentation provided in the unleash-edge repository.
Check flags
To check if a feature is enabled:
Check variants
To check variants:
Defer rendering until flags fetched
useFlagsStatus retrieves the ready state and error events. Follow the following steps in order to delay rendering until the flags have been fetched.
Unleash context
Initial context can be specified on a FlagProvider config.context property.
<FlagProvider config={{ ...config, context: { userId: 123 }}>
This code sample shows you how to update the unleash context dynamically:
Events
The core JavaScript client emits various types of events depending on internal activities. You can listen to these events by using a hook to access the client and then directly attaching event listeners. Alternatively, if you’re using the FlagProvider with a client, you can directly use this client to listen to the events. See the full list of events here.
NOTE: FlagProvider uses these internal events to provide information through useFlagsStatus.
Deferring client start
By default, the Unleash client will start polling the Proxy for toggles immediately when the FlagProvider component renders. You can prevent it by setting startClient prop to false. This is useful when you’d like to for example bootstrap the client and work offline.
Deferring the client start gives you more fine-grained control over when to start fetching the feature toggle configuration. This could be handy in cases where you need to get some other context data from the server before fetching toggles, for instance.
To start the client, use the client’s start method. The below snippet of pseudocode will defer polling until the end of the asyncProcess function.
Use Unleash client directly
Usage with class components
Since this library uses hooks you have to implement a wrapper to use with class components. Beneath you can find an example of how to use this library with class components, using a custom wrapper:
Wrap your components like so:
Developer toolbar
The Developer Toolbar provides runtime flag and context overrides during development. It wraps the standard React SDK with override capabilities while maintaining the same hook API.
Installation
Usage
Replace FlagProvider with UnleashToolbarProvider from @unleash/toolbar/react. The provider accepts the same config prop, plus optional toolbarOptions:
Key points:
- Import hooks from
@unleash/proxy-client-react(the official SDK) - The toolbar automatically handles
FlagProviderinternally - All hooks re-render automatically when you change overrides in the toolbar UI
Conditional initialization
Enable the toolbar only in development to avoid shipping it to production:
Refer to the Developer Toolbar documentation for configuration options, API reference, and the complete React example on GitHub.
React Native
For React Native and Expo applications, use the dedicated React Native SDK. It wraps this SDK and handles AsyncStorage and startTransition configuration automatically.
Migrating to v5
From v4
FlagContext public interface changed. If you used FlagContext directly you may have to adjust your code slightly to accommodate the new type changes.
From v3
The startClient option has been simplified. It now also works if you don’t pass a custom client with it. It defaults to true. The v4 major bump was driven by Node 14 end of life — no other breaking changes.
From v2
The unleash client is now a peer dependency instead of a bundled dependency. Two distribution builds are provided: one for the server (dist/index.js) and one for the browser (dist/index.browser.js). If you can’t resolve the peer dependency, run npm install unleash-proxy-client.
From v1
The built-in async storage is no longer bundled. Install a storage adapter for your preferred storage solution. No other breaking changes.