This SDK is not currently supported on Harness Self-Managed Enterprise Edition (on premises).
Deprecation Notice
This React Native SDK for Harness Feature Flags is now deprecated and will no longer be actively maintained. We encourage users to migrate to our React SDK. For more information on transitioning to the React SDK, please refer to the React SDK Documentation.
This topic describes how to use the Harness Feature Flags SDK for your React Native 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 Native SDK GitHub Repository.
Before You Begin
- Getting Started with Feature Flags
- Feature Flags Overview
- Client-Side and Server-Side SDKs
- Communication Strategy Between SDKs and Harness Feature Flags
Version
Latest SDK version can be found on GitHub Release Page
Prerequisites
To use this SDK, make sure you:
- Install React 16 or newer.
- Install React Native 0.63 or newer.
- Download the SDK from our GitHub repository
- Create a React Native application, or clone our sample application.
- Create 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
. - Create an SDK key and make a copy of it
Install the SDK
To set up the React Native SDK, complete the following steps:
Install directly to the package.json file
Install the React Native SDK by adding it to your project's package.json
file:
"ff-react-native-client-sdk": "1.0.2",
Install using npm
You can also use npm install
:
$ npm install --save ff-react-native-client-sdk
Install on IOS
For installing on iOS, run the following commands from the project root folder:
$ cd ios
$ pod install
Initialize the SDK
To initialize the React Native SDK, you need to:
- Import the
cfClientInstance
base instance. This provides all the features of the SDK. - 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 with the SDK using the Client SDK Key, Target, and Configuration parameters you set.
Import the base instance
Use the following command to import the cfClientInstance
base instance:
import cfClientInstance from 'ff-react-native-client-sdk';
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:
const apiKey = "YOUR_API_KEY";
Add a Target
Details
What is a Target?
Targets are used to control which users see which Variation of a Feature Flag, for example, if you want to do internal testing, you can enable the Flag for some users and not others. When creating a Target, you give it a name and a unique identifier. Often Targets are users but you can create a Target from anything that can be uniquely identified, such as an app or a machine.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") |
Regex requirements for Target names and identifiers
Identifier
Regex: ^[A-Za-z0-9.@_-]*$
Must consist of only alphabetical characters, numbers, and the following symbols:
. (period)
@ (at sign)
-(dash)
_ (underscore)
The characters can be lowercase or uppercase but cannot include accented letters, for example Cafe_789
.
Name
Regex: ^[\\p{L}\\d .@_-]*$
Must consist of only alphabetical characters, numbers, and the following symbols:
. (period)
@ (at sign)
-(dash)
_ (underscore)
(space)
The characters can be lowercase or uppercase and can include accented letters, for example Café_123
.
For example:
const cfTarget = new CfTarget();
cfTarget.identifier = 'HT_1';
Configure the SDK
You can configure the following features of the SDK:
Name | Example | Description | Default Value |
baseUrl | baseUrl = "``https://config.ff.harness.io/api/1.0"``; | The URL used to fetch Feature Flag Evaluations. When using the Relay Proxy, change this to: http://localhost:7000 | https://config.ff.harness.io/api/1.0 |
eventUrl | eventUrl = "``https://events.ff.harness.io/api/1.0"``; | The URL for posting metrics data to the Feature Flag service. When using the Relay Proxy, change this to: http://localhost:7000 | https://events.ff.harness.io/api/1.0 |
pollInterval | pollInterval = 60; | The interval in seconds that we poll for changes when you are using stream mode. | 60 (seconds) |
streamEnabled | streamEnabled = true; | Set to true to enable streaming mode.Set to false to disable streaming mode. | true |
analyticsEnabled | analyticsEnabled = true; | Set to true to enable analytics.Set to false to disable analytics.Note: When enabled, analytics data is posted every 60 seconds. | true |
Use cfConfiguration
to declare the configuration options you want to use, for example:
const cfConfiguration = new CfConfiguration();
cfConfiguration.streamEnabled = false;
Complete the initialization
Complete the initialization using the apiKey, cfConfiguration, and cfTarget variables, for example:
const result = await cfClientInstance.initialize(apiKey, cfConfiguration, cfTarget);
Sample of initializing the SDK
import cfClientInstance, {CfConfiguration, CfTarget} from 'ff-react-native-client-sdk';
const client = cfClientInstance;
const cfConfiguration = new CfConfiguration();
cfConfiguration.streamEnabled = true;
const cfTarget = new CfTarget();
cfTarget.identifier = 'Harness_Target_1';
const apiKey = "YOUR_API_KEY";
const result = await cfClientInstance.initialize(apiKey, cfConfiguration, cfTarget);
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.
There are different methods for the different Variation types and for each method you need to pass in:
- Identifier of the Flag you want to evaluate
- The default Variation
Evaluate a boolean Variation
//get boolean evaluation
let evaluation = await client.boolVariation("demo_bool_evaluation", false)
Evaluate a number Variation
//get number evaluation
let numberEvaluation = await client.numberVariation("demo_number_evaluation", 0)