React Client SDK Reference
This topic explains how to use the Harness Feature Flags SDK in your React Client application.
This topic describes how to use the Harness Feature Flags SDK for your React Client application.
For getting started quickly, you can use our sample code from the SDK README. You can also clone and run a sample application from the React Client SDK GitHub Repository.
Before you begin
Version
Latest SDK version can be found on GitHub Release Page
Prerequisites
To use this SDK, make sure you've:
Installed Node.js v12 or a newer version.
Installed React.js v16.7 or a newer version.
Created a Feature Flag on the Harness Platform. If you are following along with the SDK README sample code, make sure your flag is called
harnessappdemodarkmode.
Install the SDK
First, install the SDK as a dependency in your application. You can install using npm or Yarn:
Install using npm
Install using Yarn
Initialize the SDK
To initialize the React Client SDK, you need to:
Import the relevant components.
Add your Client SDK key to connect to your Harness Environment.
Add a Target that you want to Evaluate against a Feature Flag.
(Optional) Configure the SDK options.
Complete the initialization.
Import the components
Import FFContextProvider from @harnessio/ff-react-client-sdk to wrap your application. This allows you to access the Feature Flags service. For example:
Add your Client SDK key
To connect to the correct environment that you set up on the Harness Platform, you need to add the Client SDK key from that environment. Input the Client SDK key into the apiKey parameter, for example:
Add a target
For more information about targets, go to Targeting Users With Flags.
To add a target that you want to evaluate, build it using cfTarget and pass in arguments for the following:
Parameter
Description
Required?
Example
identifier
Unique ID for the target.
Required
identifier: 'HT_1',
name
Name for this target. This does not have to be unique. Note: If you don’t provide a value, the name will be the same as the identifier. Read Regex requirements for target names and identifiers below for accepted characters.
Required
name: 'Harness_Target_1'
attributes
Additional data you can store for a target, such as email addresses or location. These are key/pair values.
Optional
email: 'demo@harness.io’ location: 'EMEA'
For example:
Configure the SDK
You can configure the following features of the SDK:
Name
Description
Default Value
baseUrl
The URL used to fetch feature flag evaluations. When using the relay proxy, make sure the URL you provide is publicly available.
https://config.ff.harness.io/api/1.0
eventUrl
The URL for posting metrics data to the feature flag service. When using the relay proxy, make sure the URL you provide is publicly available.
https://events.ff.harness.io/api/1.0
streamEnabled
Set to true to enable streaming mode. Set to false to disable streaming mode.
true
Evaluate a flag
Evaluating a flag is when the SDK processes all flag rules and returns the correct variation of that flag for the target you provide.
If a matching flag can’t be found, or the SDK can’t remotely fetch flags, the default value is returned. The default value is undefined, however you can optionally set the default value to something else, which is particularly useful if you plan to use async mode on startup. For more information about async mode, go to Use async mode.
With this SDK you can evaluate a single flag or a multiple flags. To do this, import and use one of the following hooks and pass in:
Identifier of the flag you want to evaluate
(Optional) The default variation
Evaluating a single flag
Import and use useFeatureFlag, for example:
Evaluating multiple flags
Import and use useFeatureFlags, for example:
You can also return a subset of flags by using the following:
Returning a subset of flags
Returning a subset of flags with customized default values
Test your app is connected to Harness
When you receive a response showing the current status of your feature flag, go to the Harness Platform and toggle the flag on and off. Then, check your app to verify if the flag variation displayed is updated with the variation you toggled.
Close the SDK client
In most applications, you won't need to close the SDK client.
However, you should close the SDK client if:
Your application is about to terminate. Closing the client ensures that all associated resources are released.
You have determined that you do not need to evaluate flags again in your application lifecycle.
Mock the SDK when testing with Jest
When testing your React application with Jest, you may want to mock the SDK to avoid making network requests. You can do this by using the TestWrapper component included in the SDK. This component accepts a listing of flags and their values, and mocks the SDK to return those values.
Example
In the example below, we use Testing Library to render the component <MyComponent /> that internally uses the useFeatureFlag hook.
Additional options
Use async mode
By default, the React Client SDK blocks the rendering of children until the initial load of feature flags has completed. This ensures that children have immediate access to all flags when they are rendered. However, in some circumstances it may be beneficial to immediately render the application and handle the display of loading on a component-by-component basis. The React Client SDK's asynchronous mode allows this by passing the optional async prop when connecting with the FFContextProvider. For example:
When using async mode, the loading prop indicates whether the SDK has finished loading the flags. When loading completes, the loading prop will be false, and the flags prop will contain all known flags. To use this, import the withFeatureFlags higher-order component and pass in your component, for example:
When using async mode, the default value is returned until the flags are retrieved. If you use the useFeatureFlag or useFeatureFlags hooks, the default value is undefined unless you change it when evaluating the flag. If you are using withFeatureFlags, you will receive an empty object.
Display a loading component until flags are returned in async mode
When using async mode and the ifFeatureFlag HOC, by default the component won’t display until the flags are retrieved. This behavior can be overridden by passing a loadingFallback element in the options object. This will display the loadingFallback until the flags are retrieved, then the component will either show or hide as normal.
For example:
Conditionally render a component
You can conditionally render a component by using ifFeatureFlag, which is a higher-order component (HOC) that wraps your component and returns it only when the named Flag is enabled or matches a specific value.
Render if the flag is enabled
By default ifFeatureFlag will render the component if the named flag evaluates as truthy. In the below example, you can use MyConditionalComponent as a normal component and it will render the MyComponent component if the flag is enabled.
Render if the condition is matched
By default the component will display if the flag value is evaluated as truthy. This behavior can be overridden by passing the matchValue option in the options object. The flag value will then be compared with matchValue to determine whether it should display. For example:
Sample code for a React Client application
Last updated
Was this helpful?