For the complete documentation index, see llms.txt. This page is also available as Markdown.

React Native SDK

Build React Native apps with the Harness FME SDK to manage mobile feature flags and experiments for iOS and Android.

This guide provides detailed information about our React Native SDK. This SDK is built on top of our JS SDK core modules but is optimized for React Native applications. This SDK also has a pluggable API you can use to include more functionality optionally and keep your bundle leaner.

If already using our isomorphic JavaScript SDK, consider this migration guide to understand the changes of the new pluggable API.

All of our SDKs are open source. Go to our React Native SDK GitHub repository to see the source code.

MIGRATING FROM V0.X TO V1.X

Refer to this migration guide for complete information on updating to v1.x.

Before you begin

The FME SDK for React Native supports both React Native bare projects (a.k.a. React Native without a framework) and Expo managed projects.

It has been validated with React Native v0.59 and later, and Expo v36 and later, but should also work with older versions.

Initialization

Set up FME in your code base with two steps.

1. Import the SDK into your project

Install the package in your project:

npm install @splitsoftware/splitio-react-native
yarn add @splitsoftware/splitio-react-native
expo install @splitsoftware/splitio-react-native

The SDK supports two synchronization mechanisms, streaming (default and recommended) and polling which is the fallback in cases where streaming is not supported or as a temporary measure in case of any issues detected on the persistent connection. We recommend following the steps below to enable the necessary support for the Event Source modules.

  • For Expo and React Native bare projects using React Native version 0.74 or above, no additional setup is required: streaming is supported out-of-the-box using the global XMLHttpRequest object.

  • For React Native bare projects below version 0.74, we recommend linking to the native modules of the package, since streaming via XMLHttpRequest does not work on Android in debug mode.

    • If using React Native 0.59 or below, run react-native link @splitsoftware/splitio-react-native.

    • If using React Native 0.60+, the autolink feature is available and you don't need to run react-native link, but you still need to install the pods if developing for iOS, with the command npx pod-install ios.

2. Instantiate the SDK and create a new SDK factory client

NOTICE FOR TYPESCRIPT

With the SDK package you get the SplitIO namespace, which contains useful types and interfaces for you to use.

Feel free to dive into the declaration files if IntelliSense is not enough!

We recommend instantiating the SDK factory once as a singleton and reusing it throughout your application. Consider instantiating it once in the global scope, or in the componentDidMount method of your application root component.

Configure the SDK with the SDK key for the FME environment that you would like to access. In legacy Split (app.split.io) the SDK key is found on your Admin settings page, in the API keys section. Select a client-side SDK API key. This is a special type of API token with limited privileges for use in browsers or mobile clients. See API keys to learn more.

Use the SDK

Basic use

When the SDK is instantiated, it starts background tasks to update an in-memory cache with small amounts of data fetched from Harness servers. This process can take up to a few hundred milliseconds depending on the size of data. If the SDK is asked to evaluate which treatment to show to a customer for a specific feature flag while its in this intermediate state, it may not have the data necessary to run the evaluation. In this case, the SDK does not fail, rather, it returns the control treatment.

To make sure the SDK is properly loaded before asking it for a treatment, block until the SDK is ready, as shown below. We set the client to listen for the SDK_READY event triggered by the SDK before asking for an evaluation.

After the SDK_READY event fires, you can use the getTreatment method to return the proper treatment based on the FEATURE_FLAG_NAME and the key variables you passed when instantiating the SDK.

Then use an if-else-if block as shown below and insert the code for the different treatments that you defined in Harness FME. Remember the final else branch in your code to handle the client returning control.

NOTICE WHEN DEBUGGING IN ANDROID

When running your app in debug mode on an Android device or emulator, you might get a warning notification stating that "Setting a timer for a long period of time is a performance and correctness issue on Android".

The warning is explained here. It is intended to make developers aware that timer callbacks are invoked in foreground, and therefore timers could be delayed while the app is in background.

Since the SDK uses timers for periodically pushing data to Harness FME servers, it is acceptable if those operations are delayed while the app is in background, and so it is completely safe to ignore or hide this warning. If there is any concern, feel free to contact us through support.

Attribute syntax

To target based on custom attributes, the SDK's getTreatment method needs to be passed an attribute map at runtime.

In the example below, we are rolling out a feature flag to users. The provided attributes plan_type, registered_date, permissions, paying_customer, and deal_size are passed to the getTreatment call. These attributes are compared and evaluated against the attributes used in the rollout plan as defined in Harness FME to decide whether to show the on or off treatment to this account.

The getTreatment method has a number of variations that are described below. Each of these additionally has a variation that takes an attributes argument, which can defines attributes of the following types: strings, numbers, dates, booleans, and sets. The proper data type and syntax for each are:

  • Strings: Use type String.

  • Numbers: Use type Number.

  • **Dates: ** Use type Date and express the value in milliseconds since epoch. Note: Milliseconds since epoch is expressed in UTC. If your date or date-time combination is in a different timezone, first convert it to UTC, then transform it to milliseconds since epoch.

  • Booleans: Use type Boolean.

  • Sets: Use type Array.

You can pass your attributes in exactly this way to the client.getTreatments method.

Binding attributes to the client

Attributes can optionally be bound to the client at any time during the SDK lifecycle. These attributes are stored in memory and used in every evaluation to avoid the need to keep the attribute set accessible through the whole app. When an evaluation is called, the attributes provided (if any) at evaluation time are combined with the ones that are already loaded into the SDK memory, with the ones provided at function execution time taking precedence. This enables those attributes to be overridden or hidden for specific evaluations.

An attribute is considered valid if it follows one of the types listed below:

  • String

  • Number

  • Boolean

  • Array

The SDK validates these before storing them and if there are invalid or missing values, possibly indicating an issue, the methods return the boolean false and do not update any value.

To use these methods, refer to the example below:

Multiple evaluations at once

In some instances, you may want to evaluate treatments for multiple feature flags at once. Use the different variations of getTreatments from the SDK factory client to do this.

  • getTreatments: Pass a list of the feature flag names you want treatments for.

  • getTreatmentsByFlagSet: Evaluate all flags that are part of the provided set name and are cached on the SDK instance.

  • getTreatmentsByFlagSets: Evaluate all flags that are part of the provided set names and are cached on the SDK instance.

Get treatments with configurations

To leverage dynamic configurations with your treatments, you should use the getTreatmentWithConfig method.

This method returns an object with the structure below:

As you can see from the object structure, the config will be a stringified version of the configuration JSON defined in Harness FME. If there is no configuration defined for a treatment, the SDK will return null for the config parameter.

This method takes the exact same set of arguments as the standard getTreatment method. See below for examples on proper usage:

If you need to get multiple evaluations at once, you can also use the getTreatmentsWithConfig methods. These methods take the exact same arguments as the getTreatments methods but return a mapping of feature flag names to TreatmentResults instead of strings. Example usage below:

If a flag cannot be evaluated, the SDK returns the fallback treatment value (default "control" unless overridden globally or per flag). For more information, see Fallback treatments.

Append properties to impressions

Impressions are generated by the SDK each time a getTreatment method is called. These impressions are periodically sent back to Harness servers for feature monitoring and experimentation.

You can append properties to an impression by passing an object of key-value pairs to the getTreatment method. These properties are then included in the impression sent by the SDK and can provide useful context to the impression data.

Three types of properties are supported: strings, numbers, and booleans.

Shutdown

You can call the client.destroy() method to gracefully shut down the SDK by stopping all background threads, clearing caches, closing connections, and flushing the remaining unpublished impressions. If the SDK was instantiated in the componentDidMount method of a React component, destroy should be called in the corresponding componentWillUnmount method.

However while releasing resources if the SDK is not needed anymore is a good practice, since the SDK automatically hooks to application state transitions (foreground, background) data synchronization is managed by the SDK and pending events are flushed automatically.

After destroy() is called and finishes, any subsequent invocations to getTreatment/getTreatments or manager methods result in control or empty list, respectively.

Track

Use the track method to record any actions your customers perform. Each action is known as an event and corresponds to an event type. Calling track through one of our SDKs or via the API is the first step to getting experimentation data into Harness FME and allows you to measure the impact of your feature flags on your users’ actions and metrics.

Learn more about using track events in feature flags.

In the examples below you can see that the .track() method can take up to four arguments. The proper data type and syntax for each are:

  • TRAFFIC_TYPE: The traffic type of the key in the track call. The expected data type is String. You can only pass values that match the names of traffic types that you have defined Harness FME.

  • EVENT_TYPE: The event type that this event should correspond to. The expected data type is String. Full requirements on this argument are:

    • Contains 63 characters or fewer.

    • Starts with a letter or number.

    • Contains only letters, numbers, hyphen, underscore, or period.

    • This is the regular expression we use to validate the value: [a-zA-Z0-9][-_\.a-zA-Z0-9]{0,62}

  • VALUE: (Optional) The value to be used in creating the metric. This field can be sent in as null or 0 if you intend to purely use the count function when creating a metric. The expected data type is Integer or Float.

  • PROPERTIES: (Optional) An object of key value pairs that can be used to filter your metrics. Learn more about event property capture in the Events guide. FME currently supports three types of properties: strings, numbers, and booleans.

The track method returns a boolean value of true or false to indicate whether or not the SDK was able to successfully queue the event to be sent back to Harness servers on the next event post. The SDK returns false if the current queue size is equal to the config set by eventsQueueSize or if an incorrect input to the track method has been provided.

In the case that a bad input has been provided, you can read more about our SDK's expected behavior here.

Configuration

The SDK has a number of knobs for configuring performance. Each knob is tuned to a reasonable default. However, you can override the value while instantiating the SDK. The parameters available for configuration are shown below.

Configuration

Description

Default value

core.labelsEnabled

Enable impression labels from being sent to Harness FME's backend. Labels may contain sensitive information.

true

startup.readyTimeout

Maximum amount of time in seconds to wait before firing the SDK_READY_TIMED_OUT event

10

startup.requestTimeoutBeforeReady

The SDK has two main endpoints it uses /splitChanges and /memberships that it hits to get ready. This config sets how long (in seconds) the SDK will wait for each request it makes as part of getting ready.

5

startup.retriesOnFailureBeforeReady

How many retries on /splitChanges and /memberships we will do while getting the SDK ready

1

startup.eventsFirstPushWindow

Use to set a specific timer (expressed in seconds) for the first push of events, starting on SDK initialization.

10

scheduler.featuresRefreshRate

The SDK polls Harness servers for changes to feature rollout plans. This parameter controls this polling period in seconds.

60

scheduler.segmentsRefreshRate

The SDK polls Harness servers for changes to segment definitions. This parameter controls this polling period in seconds.

60

scheduler.impressionsRefreshRate

The SDK sends information on who got what treatment at what time back to Harness servers to power analytics. This parameter controls how often this data is sent to Harness servers. The parameter should be in seconds.

300

scheduler.impressionsQueueSize

The max amount of impressions we queue. If the queue is full, the SDK flushes the impressions and resets the timer.

30000

scheduler.eventsPushRate

The SDK sends tracked events to Harness servers. This setting controls that flushing rate in seconds.

60

scheduler.eventsQueueSize

The max amount of events we queue. If the queue is full, the SDK flushes the events and resets the timer.

500

scheduler.telemetryRefreshRate

The SDK caches diagnostic data that it periodically sends to Harness servers. This configuration controls how frequently this data is sent back to Harness servers (in seconds).

3600 seconds (1 hour)

sync.splitFilters

Filter specific feature flags to be synced and evaluated by the SDK. This is formed by a type string property and a list of string values for the given criteria. Using the types 'bySet' (recommended, flag sets are available in all tiers) or 'byName', pass an array of strings defining the query. If empty or unset, all feature flags are downloaded by the SDK.

[]

sync.impressionsMode

This configuration defines how impressions (decisioning events) are queued on the SDK. Supported modes are OPTIMIZED, NONE, and DEBUG. In OPTIMIZED mode, only unique impressions are queued and posted to Harness; this is the recommended mode for experimentation use cases. In NONE mode, no impression is tracked in Harness FME and only minimum viable data to support usage stats is, so never use this mode if you are experimenting with that instance impressions. Use NONE when you want to optimize for feature flagging only use cases and reduce impressions network and storage load. In DEBUG mode, ALL impressions are queued and sent to Harness; this is useful for validations. This mode doesn't impact the impression listener which receives all generated impressions locally.

OPTIMIZED

sync.enabled

Controls the SDK continuous synchronization flags. When true, a running SDK processes rollout plan updates performed in Harness FME (default). When false, it fetches all data upon init, which ensures a consistent experience during a user session and optimizes resources when these updates are not consumed by the app.

true

sync.requestOptions.getHeaderOverrides

A callback function that can be used to override the Authentication header or append new headers to the SDK's HTTP(S) requests.

undefined

debug

Either a boolean flag, string log level or logger instance for activating SDK logs. See logging for details.

false

streamingEnabled

Boolean flag to enable the streaming service as default synchronization mechanism. In the event of an issue with streaming, the SDK will fallback to the polling mechanism. If false, the SDK will poll for changes as usual without attempting to use streaming.

true

userConsent

User consent status used to control the tracking of events and impressions. Possible values are GRANTED, DECLINED, and UNKNOWN. See User consent for details.

GRANTED

fallbackTreatments

Configure fallback treatments for the SDK.

undefined

To set each of the parameters defined above, use the following syntax.

Configure cache behavior

To use the pluggable InLocalStorage option of the SDK and be able to cache flags for subsequent loads in the same browser, you need to pass it to the SDK config on its storage option.

This InLocalStorage function accepts an optional object with options described below:

Configuration

Description

Default value

prefix

An optional prefix for your data, to avoid collisions. This prefix is prepended to the existing "SPLITIO" localStorage prefix.

SPLITIO

expirationDays

Number of days before cached data expires if it was not updated. If cache expires, it is cleared when the SDK is initialized.

10

clearOnInit

When set to true, the SDK clears the cached data on initialization unless it was cleared within the last 24 hours. This 24-hour window is not configurable. If the cache is cleared (whether due to expiration or clearOnInit), both the 24-hour period and the expirationDays period are reset.

false

wrapper

Storage wrapper used to persist the SDK cached data.

localStorage

By default, the SDK uses the localStorage global object if available. If it is not available, the SDK will use the default in memory storage.

To support a persistent cache on platforms like Android and iOS, where localStorage is not available, you can pass your own storage wrapper, such as AsyncStorage, that implements the SplitIO.StorageWrapper interface.

Localhost mode

For testing, a developer can put code behind feature flags on their development machine without the SDK requiring network connectivity. To achieve this, the SDK can be started in localhost mode (aka off-the-grid or offline mode). In this mode, the SDK neither polls nor updates Harness servers. Instead, it uses an in-memory data structure to determine what treatments to show to the logged in customer for each of the features.

Define the feature flags you want to use in the features object map. All getTreatment calls for a feature flag now only return the one treatment (and config, if defined) that you have defined in the map. You can then change the treatment as necessary for your testing. To update a treatment or a config, or to add or remove feature flags from the mock cache, update the properties of the features object you've provided. The SDK simulates polling for changes and updates from it. Do not assign a new object to the features property because the SDK has a reference to the original object and will not detect the change.

Any feature that is not provided in the features map returns the control treatment if the SDK was asked to evaluate them.

You can use the additional configuration parameters below when instantiating the SDK in localhost mode.

Configuration

Description

Default value

scheduler.offlineRefreshRate

The refresh interval for the mocked features treatments.

15

features

A fixed mapping of which treatment to show for our mocked features.

{} By default we have no mocked features.

To use the SDK in localhost mode, replace the SDK Key on authorizationKey property with 'localhost', as shown in the example below. Note that you can define in the features object a feature flag name and its treatment directly or use a map to define both a treatment and a dynamic configuration.

If you define just a string as the value for a feature flag name, any config returned by our SDKs will always be null. If you use a map, we return the specified treatment and the specified config (which can also be null).

TESTING WITH JEST

We recommend using the SDK in localhost mode for your tests.

For example, you can mock the module import (see Jest documentation for details) to instantiate the SDK in localhost mode as shown below:

It is not recommended to use the default (online) mode of the SDK in your tests because it slows them down and increases their instability due to network latencies. However, if you must use it, you need to polyfill the Fetch API, which is used by the SDK but not provided by Jest and Node.js. isomorphic-fetch is a good option for that.

Manager

Use the Split Manager to get a list of features available to the SDK factory client.

To instantiate a Manager in your code base, use the same factory that you used for your client.

The Manager then has the following methods available.

The SplitView object referenced above has the following structure:

Listener

FME SDKs send impression data back to Harness servers periodically and as a result of evaluating feature flags. To additionally send this information to a location of your choice, define and attach an impression listener. For that purpose, the SDK's configurations have a parameter called impressionListener where an implementation of ImpressionListener could be added. This implementation must define the logImpression method and it receives data in the following schema.

Name

Type

Description

impression

Object

Impression object that has the feature, key, treatment, label, etc.

attributes

Object

A map of attributes passed to getTreatment/getTreatments (if any).

sdkLanguageVersion

String

The version of the SDK. In this case the language is reactnative plus the version currently running.

NOTE

There are two additional keys on this object, ip and hostname. They are not captured on the client side but kept for consistency.

Implement custom impression listener

Here is an example of how implement a custom impression listener.

An impression listener is called asynchronously from the corresponding evaluation, but is almost immediate.

Even though the SDK does not fail if there is an exception in the listener, do not block the call stack.

Logging

To trim as many bits as possible from the user application builds, we divided the logger in implementations that contain the log messages for each log level: ErrorLogger, WarnLogger, InfoLogger, and DebugLogger. Higher log level options contain the messages for the lower ones, with DebugLogger containing them all. Thus, to enable descriptive SDK logging you need to plug in a logger instance as shown below:

You can also enable the SDK logging via a boolean or log level value as debug settings, and change it dynamically by calling the SDK Logger API.

However, in any case where the proper logger instance is not plugged in, instead of a human readable message you'll get a code and optionally some params for the log itself. While these logs would be enough for the Split support team, if you find yourself in a scenario where you need to parse this information, you can check the constant files in our javascript-commons repository (where you have tags per version if needed) under the logger folder.

By default, the SDK uses the console.log method to output log messages for all log levels.

Since v1.4.0 of the SDK, you can provide a custom logger to handle SDK log messages by setting the logger configuration option or using the factory.Logger.setLogger method.

The logger object must implement the SplitIO.Logger interface, which is compatible with the console object and logging libraries such as winston, pino, and log4js. The interface is defined as follows:

The following example passes the console object as a logger, so that console.error, console.warn, console.info, and console.debug methods are called rather than the default console.log method.

Configure fallback treatments

Fallback treatments let you define a treatment value (and optional configuration) to be returned when a flag cannot be evaluated. By default, the SDK returns control, but you can override this globally or for individual flags at the SDK level.

This is useful when you want to:

  • Maintain a predictable user experience during outages or evaluation failures (avoid unexpected control in production)

  • Protect critical user flows by returning a safe, stable treatment (for example, forcing off during an incident)

  • Customize behavior per flag so each evaluation inherits appropriate safe defaults if something goes wrong

Global fallback treatment

Set a global fallback treatment when initializing the SDK factory. This value is returned whenever any flag cannot be evaluated.

Flag-level fallback treatment

You can also set a fallback treatment per flag when calling getTreatment or getTreatmentWithConfig. This flag-level fallback always takes precedence over the global fallback treatment, so if both are defined, the SDK will return the flag-level value when that flag cannot be evaluated.

For more information, see Fallback treatments.

Advanced use cases

This section describes advanced use cases and features provided by the SDK.

Instantiate multiple SDK clients

FME supports the ability to release based on multiple traffic types. For example, with traffic types, you can release to users in one feature flag and accounts in another. If you are unfamiliar with using multiple traffic types, refer to the Traffic type guide for more information.

Each SDK factory client is tied to one specific customer ID at a time, so if you need to roll out feature flags by different traffic types, instantiate multiple SDK clients, one for each traffic type. For example, you may want to roll out the feature user-poll by users and the feature account-permissioning by accounts.

You can do this with the example below.

NUMBER OF SDK INSTANCES

While the SDK does not put any limitations on the number of instances that can be created, we strongly recommend keeping the number of SDKs down to one or two.

Subscribe to events

You can listen for four different events from the SDK.

  • SDK_READY_FROM_CACHE. This event fires when the SDK is ready to evaluate treatments. If the SDK is configured to use a persistent cache using the InLocalStorage module (see Configure cache behavior), it will attempt to use a locally cached version of your rollout plan from a previous session. If data is cached, this event fires almost immediately, since access to the cache is fast, but data might be stale. Otherwise, it fires together with the SDK_READY event when the SDK downloads the rollout plan from Harness servers.

  • SDK_READY. This event fires once the SDK is ready to evaluate treatments using the most up-to-date version of your rollout plan, downloaded from Harness servers.

  • SDK_READY_TIMED_OUT. This event fires if the SDK could not download the data from Harness servers (SDK_READY event), within the time specified by the startup.readyTimeout configuration parameter. This event does not indicate that the SDK initialization was interrupted. The SDK continues downloading the rollout plan and fires the SDK_READY event when finished. This delayed SDK_READY event may happen with slow connections or large rollout plans with many feature flags, segments, or dynamic configurations.

  • SDK_UPDATE. This event fires whenever your rollout plan is changed. Listen for this event to refresh your app whenever a feature flag or segment is changed in Harness FME.

The syntax to listen for each event is shown below:

Using readiness state and promises

The SDK_READY_FROM_CACHE, SDK_READY, and SDK_READY_TIMED_OUT events fire only once. Therefore, if an event listener is attached after the event has already fired, it will never be triggered.

For this reason, you can check the SDK readiness state using the client.getStatus() method to determine whether the SDK is ready to evaluate treatments, among other things:

As an alternative to event listeners, you can also use the client promise methods whenReady and whenReadyFromCache to wait for the SDK to become ready.

  • The whenReadyFromCache() promise resolves once the SDK_READY_FROM_CACHE event is emitted, or rejects if the SDK_READY_TIMED_OUT event is emitted first.

  • The whenReady() promise resolves when the SDK_READY event is emitted, or rejects if the SDK_READY_TIMED_OUT event is emitted first. Subsequent calls to client.whenReady() may return a new promise with a different settled state. For instance, a resolved promise if the SDK becomes ready after the SDK_READY_TIMED_OUT event was triggered first.

The SDK allows you to disable the tracking of events and impressions until user consent is explicitly granted or declined.

The userConsent configuration parameter lets you set the initial consent status of the SDK instance, and the factory method UserConsent.setStatus(boolean) lets you grant (enable) or decline (disable) dynamic data tracking.

There are three possible initial states:

  • 'GRANTED': The user grants consent for tracking events and impressions. The SDK sends them to Harness FME servers. This is the default value if userConsent param is not defined.

  • 'DECLINED': The user declines consent for tracking events and impressions. The SDK does not send them to Harness FME servers.

  • 'UNKNOWN': The user neither grants nor declines consent for tracking events and impressions. The SDK tracks them in its internal storage, and eventually either sends them or not if the consent status is updated to 'GRANTED' or 'DECLINED' respectively.

The status can be updated at any time with the UserConsent.setStatus factory method.

Working with user consent is demonstrated below.

Usage with React SDK

The React SDK is a wrapper around the JavaScript SDK that provides a more React-friendly API based on React components and hooks. You can use the React Native SDK with the React SDK in your React Native application.

For an example application detailing how to configure and instantiate the Split React Native SDK, see the React Native & Expo examples GitHub repository.

Last updated

Was this helpful?