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
---
2021-12-06 13:16:54 +01:00
This library is meant to be used with the [unleash-proxy ](https://github.com/Unleash/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.
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-06-29 13:04:21 +02:00
Your proxy's URL.
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 = {
url: 'https://HOSTNAME/api/proxy',
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
```