2021-08-17 15:24:37 +02:00
---
id: proxy-react
2022-06-29 13:04:21 +02:00
title: React Proxy SDK
2021-08-17 15:24:37 +02:00
---
2022-09-15 09:02:10 +02:00
This library can be used with the [Unleash Proxy ](https://github.com/Unleash/unleash-proxy ) or with the [Unleash front-end API ](../reference/front-end-api ). It is _not_ compatible with the regular Unleash client API.
2021-08-17 15:24:37 +02:00
2022-06-29 13:04:21 +02:00
For more detailed information, check out the [React Proxy SDK on GitHub ](https://github.com/Unleash/proxy-client-react ).
2021-12-06 13:16:54 +01:00
## Installation
2022-05-10 13:26:52 +02:00
Install the React proxy client and the [JavaScript proxy client ](proxy-javascript.md ) packages:
2022-01-03 14:59:47 +01:00
```shell npm2yarn
2022-05-10 13:26:52 +02:00
npm install @unleash/proxy -client-react unleash-proxy-client
2021-08-17 15:24:37 +02:00
```
2022-04-28 15:33:38 +02:00
## Initialization
2021-12-06 13:16:54 +01:00
The snippet below shows you how to initialize the client. We recommend that you do this in your entry point file (typically index.js/ts) to ensure that you only have _one_ instance of it.
The configuration variables are:
2022-06-29 13:04:21 +02:00
2021-12-06 13:16:54 +01:00
- **`url`**
2022-09-15 09:02:10 +02:00
Your proxy's URL or the Unleash front-end API endpoint (`< unleash-url > /api/frontend`).
2022-06-29 13:04:21 +02:00
2021-12-06 13:16:54 +01:00
- **`clientKey`**
2022-06-29 13:04:21 +02:00
One of your proxy's [designated client keys (also known as proxy secrets) ](unleash-proxy#configuration-variables ).
2021-12-06 13:16:54 +01:00
- **`refreshInterval`**
2021-08-17 15:24:37 +02:00
2021-12-06 13:16:54 +01:00
How often (in seconds) the client should poll the proxy for updates.
- **`appName`**
The name of your application. It's only used for identifying your application and can be whatever you want it to be.
- **`environment`**
The environment that your application runs in. This corresponds to the environment field in [the Unleash Context ](../user_guide/unleash-context.md ). Note that this is separate from the newer [Environments feature ](../user_guide/environments.md ).
```jsx
2022-05-20 08:48:01 +02:00
import { FlagProvider } from '@unleash/proxy-client-react';
2021-08-17 15:24:37 +02:00
const config = {
2022-09-15 09:02:10 +02:00
url: 'https://PROXY_HOSTNAME/api/proxy', // or https://UNLEASH_HOSTNAME/api/frontend
2021-08-17 15:24:37 +02:00
clientKey: 'PROXYKEY',
refreshInterval: 15,
appName: 'your-app-name',
environment: 'dev',
};
ReactDOM.render(
< React.StrictMode >
< FlagProvider config = {config} >
< App / >
< / FlagProvider >
< / React.StrictMode > ,
2022-06-29 13:04:21 +02:00
document.getElementById('root'),
2021-08-17 15:24:37 +02:00
);
```
2021-12-06 13:16:54 +01:00
## How to check feature toggle states
2021-08-17 15:24:37 +02:00
To check if a feature is enabled:
2021-12-06 13:16:54 +01:00
```jsx
2021-08-17 15:24:37 +02:00
import { useFlag } from '@unleash/proxy-client-react';
const TestComponent = () => {
const enabled = useFlag('travel.landing');
if (enabled) {
2022-06-29 13:04:21 +02:00
return < SomeComponent / > ;
2021-08-17 15:24:37 +02:00
}
2022-06-29 13:04:21 +02:00
return < AnotherComponent / > ;
2021-08-17 15:24:37 +02:00
};
export default TestComponent;
```
To check variants:
2021-12-06 13:16:54 +01:00
```jsx
2021-08-17 15:24:37 +02:00
import { useVariant } from '@unleash/proxy-client-react';
const TestComponent = () => {
const variant = useVariant('travel.landing');
2022-06-29 13:04:21 +02:00
if (variant.enabled & & variant.name === 'SomeComponent') {
return < SomeComponent / > ;
} else if (variant.enabled & & variant.name === 'AnotherComponent') {
return < AnotherComponent / > ;
2021-08-17 15:24:37 +02:00
}
2022-06-29 13:04:21 +02:00
return < DefaultComponent / > ;
2021-08-17 15:24:37 +02:00
};
export default TestComponent;
```
2021-12-06 13:16:54 +01:00
## How to update the Unleash Context
2021-08-17 15:24:37 +02:00
Follow the following steps in order to update the unleash context:
2021-12-06 13:16:54 +01:00
```jsx
2022-06-29 13:04:21 +02:00
import { useUnleashContext, useFlag } from '@unleash/proxy-client-react';
2021-08-17 15:24:37 +02:00
const MyComponent = ({ userId }) => {
2022-06-29 13:04:21 +02:00
const variant = useFlag('my-toggle');
2021-08-17 15:24:37 +02:00
const updateContext = useUnleashContext();
useEffect(() => {
// context is updated with userId
2022-06-29 13:04:21 +02:00
updateContext({ userId });
}, []);
};
2021-08-17 15:24:37 +02:00
```