React SDK
This React SDK is designed to help you integrate with Unleash and evaluate feature flags inside your application.
You can use this client with Unleash Enterprise or Unleash Open Source.
Installation
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.
How to Use
This library uses the core unleash-js-sdk client as a base.
Initialize the Client
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):
Connection Options
To connect this SDK to your Unleash instance’s front-end API, use the URL to your Unleash instance’s front-end API (<unleash-url>/api/frontend) as the url parameter. For the clientKey parameter, use a FRONTEND token generated from your Unleash instance. Refer to the API tokens documentation for more information.
To connect this SDK to unleash-edge, follow the documentation provided in the unleash-edge repository.
Check Feature Toggle Status
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.
Updating 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:
Listening to 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.
Advanced Use Cases
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:
React Native
localStorage
IMPORTANT: This no longer comes included in the unleash-proxy-client-js library. You will need to install the storage adapter for your preferred storage solution.
Because React Native doesn’t run in a web browser, it doesn’t have access to the localStorage API. Instead, you need to tell Unleash to use your specific storage provider. The most common storage provider for React Native is AsyncStorage.
To configure it, add the following property to your configuration object:
startTransition
If your version of React Native doesn’t support startTransition, you can provide fallback implementation:
Migration Guide
Upgrade Path From v1 -> v2
If you were previously using the built in Async storage used in the unleash-proxy-client-js, this no longer comes bundled with the library. You will need to install the storage adapter for your preferred storage solution. Otherwise there are no breaking changes.
Upgrade Path From v2 -> v3
Previously the unleash client was bundled as dependency directly in this library. It’s now changed to a peer dependency and listed as an external.
In v2 there was only one distribution based on the fact that webpack polyfilled the necessary features in v4. This is no longer the case in webpack v5. We now provide two distribution builds, one for the server and one for the client - and use the browser field in the npm package to hint module builders about which version to use. The default dist/index.js file points to the node version, while the web build is located at dist/index.browser.js
Upgrading should be as easy as running yarn again with the new version, but we made the made bump regardless to be safe. Note: If you are not able to resolve the peer dependency on unleash-proxy-client you might need to run npm install unleash-proxy-client
Upgrade Path From v3 -> v4
startClient option has been simplified. Now it will also work if you don’t pass custom client with it. It defaults to true.
Note on v4.0.0:
The major release is driven by Node14 end of life and represents no other changes. From this version onwards we do not guarantee that this library will work server side with Node 14.
Upgrade Path From v4 -> v5
FlagContext public interface changed. If you used FlagContext directly you may have to adjust your code slightly to accommodate the new type changes.
Design Philosophy
This feature flag SDK is designed according to our design philosophy. You can read more about that here.