
Product
Introducing Webhook Events for Alert Changes
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.
@unleash/proxy-client-react
Advanced tools
This library is meant to be used with the unleash-proxy. The proxy application layer will sit between your unleash instance and your client applications, and provides performance and security benefits. DO NOT TRY to connect this library directly to the unleash instance, as the datasets follow different formats because the proxy only returns evaluated toggle information.
npm install @unleash/proxy-client-react
// or
yarn add @unleash/proxy-client-react
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.
Import the provider like this in your entrypoint file (typically index.js/ts):
import FlagProvider from '@unleash/proxy-client-react';
const config = {
url: 'https://HOSTNAME/proxy',
clientKey: 'PROXYKEY',
refreshInterval: 15,
appName: 'your-app-name',
environment: 'dev',
};
ReactDOM.render(
<React.StrictMode>
<FlagProvider config={config}>
<App />
</FlagProvider>
</React.StrictMode>,
document.getElementById('root')
);
Alternatively, you can pass your own client in to the FlagProvider:
import FlagProvider, { UnleashClient } from '@unleash/proxy-client-react';
const config = {
url: 'https://HOSTNAME/proxy',
clientKey: 'PROXYKEY',
refreshInterval: 15,
appName: 'your-app-name',
environment: 'dev',
};
const client = new UnleashClient(config);
ReactDOM.render(
<React.StrictMode>
<FlagProvider unleashClient={client}>
<App />
</FlagProvider>
</React.StrictMode>,
document.getElementById('root')
);
By default, the Unleash client will start polling the Proxy for toggles immediately when the FlagProvider component renders. You can delay the polling by:
startClient prop to falseFlagProviderReactDOM.render(
<React.StrictMode>
<FlagProvider unleashClient={client} startClient={false}>
<App />
</FlagProvider>
</React.StrictMode>,
document.getElementById('root')
);
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.
const client = new UnleashClient({ /* ... */ })
useEffect(() => {
const asyncProcess = async () => {
// do async work ...
client.start()
}
asyncProcess()
}, [])
return (
// Pass client as `unleashClient` and set `startClient` to `false`
<FlagProvider unleashClient={client} startClient={false}>
<App />
</FlagProvider>
)
To check if a feature is enabled:
import { useFlag } from '@unleash/proxy-client-react';
const TestComponent = () => {
const enabled = useFlag('travel.landing');
if (enabled) {
return <SomeComponent />
}
return <AnotherComponent />
};
export default TestComponent;
To check variants:
import { useVariant } from '@unleash/proxy-client-react';
const TestComponent = () => {
const variant = useVariant('travel.landing');
if (variant.enabled && variant.name === "SomeComponent") {
return <SomeComponent />
} else if (variant.enabled && variant.name === "AnotherComponent") {
return <AnotherComponent />
}
return <DefaultComponent />
};
export default TestComponent;
useFlagsStatus retrieves the ready state and error events. Follow the following steps in order to delay rendering until the flags have been fetched.
import { useFlagsStatus } from '@unleash/proxy-client-react'
const MyApp = () => {
const { flagsReady, flagsError } = useFlagsStatus();
if (!flagsReady) {
return <Loading />
}
return <MyComponent error={flagsError}/>
}
Follow the following steps in order to update the unleash context:
import { useUnleashContext, useFlag } from '@unleash/proxy-client-react'
const MyComponent = ({ userId }) => {
const variant = useFlag("my-toggle");
const updateContext = useUnleashContext();
useEffect(() => {
// context is updated with userId
updateContext({ userId })
}, [userId])
useEffect(() => {
async function run() {
// Can wait for the new flags to pull in from the different context
await updateContext({ userId });
console.log('new flags loaded for', userId);
}
run();
}, [userId]);
}
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:
const config = {
storageProvider: {
save: (name, data) => AsyncStorage.setItem(name, JSON.stringify(data)),
get: async (name) => {
const data = await AsyncStorage.getItem(name);
return data ? JSON.parse(data) : undefined;
}
},
};
react-feature-toggles is a lightweight library for managing feature toggles in React applications. It provides a simple API for defining and using feature toggles, but it does not offer the same level of integration with a feature management system like Unleash.
launchdarkly-react-client-sdk is the official React SDK for LaunchDarkly, a feature management platform. It offers similar functionality to @unleash/proxy-client-react, including feature flag evaluation and context management, but it is tied to the LaunchDarkly service.
react-ab-test is a library for A/B testing in React applications. It allows developers to define experiments and variants, and track user interactions. While it focuses on A/B testing rather than feature toggles, it provides similar capabilities for experimentation.
FAQs
React interface for working with unleash
The npm package @unleash/proxy-client-react receives a total of 230,325 weekly downloads. As such, @unleash/proxy-client-react popularity was classified as popular.
We found that @unleash/proxy-client-react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.

Product
Socket now scans OpenVSX extensions, giving teams early detection of risky behaviors, hidden capabilities, and supply chain threats in developer tools.