Getting started - React Native

Best practice: Initialize the Experiences SDK as early as possible when your app loads (for example, at app startup before you mount widgets). Early initialization improves performance and keeps analytics stable.

Sample app

The public React Native sample app shows a working Experiences SDK integration: SDK initialization, Stories, Moments, and Videos widgets, and the player entry points. Use it as the reference implementation for every feature on these pages. To build it on your machine, see Run the sample app.

For all sample apps across platforms, see Sample apps.

Prerequisites

You must get a WSC Sports API key before you begin.

On Android, React Native SDK 1.21.0 and later need your app to compile with compileSdk 35 or higher: the native Android library's minCompileSdk moved from 33 to 35.

Install the React Native SDK

npm install @wscsports/blaze-rtn-sdk@latest

Over-the-air updates

The SDK JavaScript and native code ship as one version and are supported only together. An over-the-air (OTA) update, such as EAS Update or CodePush, that changes the @wscsports/blaze-rtn-sdk version must target a binary built with that same version.

  • EAS Update: bump runtimeVersion together with the SDK, or use the fingerprint policy.
  • CodePush: bump targetBinaryVersion together with the SDK.

How to use

Import the SDK

import BlazeSDK, {
  InitOptions,
} from '@wscsports/blaze-rtn-sdk';

SDK configuration

const blazeSdkInitOptions: InitOptions = {
  apiKey: '<API_KEY>',
  externalUserToken: '<EXTERNAL_USER_TOKEN>',
  cachingSize: 512,
  cachingLevel: 'DEFAULT',
  forceLayoutDirection: 'rtl',
  restoreExternalUserId: true,
  appOverridesCTAHandling: false,
  interactionLinkHandling: 'Web',
  defaultStoryPlayerStyle: storyPlayerStyle,
  defaultMomentsPlayerStyle: momentsPlayerStyle,
  defaultVideosPlayerStyle: videosPlayerStyle,
  globalDelegate: globalDelegate,
  playerEntryPointDelegate: entryPointDelegate
};

forceLayoutDirection

Set forceLayoutDirection to 'ltr' or 'rtl' to force the SDK screens (players, widgets, and search) into that layout direction, regardless of the device's system locale. When omitted, the SDK follows the system layout direction.

forceLayoutDirection is an initialization-time option only: it takes effect when the SDK is initialized and can't be changed afterward.

externalUserToken

Available from React Native SDK 1.21.0. The viewer token for content entitlement checks. See Content entitlements. In React Native, onContentRestricted is a notification and returns nothing, and reasons arrive as strings such as 'ExternalUserTokenMissing'.

The SDK holds the token in memory only, so supply it on every launch: here, or through setExternalUserToken once init resolves. Don't call setExternalUserToken before init: if init then runs without externalUserToken, Android clears the token, while iOS keeps it.

interactionLinkHandling

Available from React Native SDK 1.21.0. Decides what the SDK does with a link that an Engagement Layer interaction inside a player asks to open:

ValueDescription
'Deeplink'Opens the link externally, for example, in the device browser. This is the default.
'Web'Opens the link in the SDK in-app web view.
'Handled'Does nothing, since your app handles the link.

When omitted, the SDK opens the link externally. A widget's own interactionLinkHandling prop takes precedence for that widget's players, including its Moments tabs player. See interactionLinkHandling on widgets.

The SDK reports each link through onTriggerInteractionLink. That callback is a notification only: open the link yourself only when you declared 'Handled'. See onTriggerInteractionLink.

appOverridesCTAHandling

Decides who opens the link when a viewer taps a CTA button in a player opened from an entry point, such as playStory or playMoment. For players opened from a widget, set the widget's own appOverridesCTAHandling prop.

ValueDescription
falseThe SDK opens the link. A 'Deeplink' CTA opens externally, through the device, and a 'Web' CTA opens in the SDK in-app web view. This is the default.
trueThe SDK opens nothing. Your app opens the link for both 'Deeplink' and 'Web' CTAs.

The SDK doesn't close the player on a CTA tap, with either value. If a 'Deeplink' CTA navigates to a screen in your app, the player can stay open on top of that screen until the viewer closes it. To show the screen right away, set appOverridesCTAHandling to true, then close the player with dismissPlayer before you navigate.

The SDK reports each tap through onTriggerCTA. That callback is a notification only: its return value doesn't change what the SDK does.

import { Linking } from 'react-native';
import BlazeSDK from '@wscsports/blaze-rtn-sdk';

const handleCta = async (actionType: string, actionParam: string) => {
  if (actionType === 'Deeplink') {
    await BlazeSDK.dismissPlayer();
    navigateToScreen(actionParam); // Your app's navigation
  } else {
    Linking.openURL(actionParam);
  }
};

await BlazeSDK.init({
  apiKey: '<API_KEY>',
  appOverridesCTAHandling: true,
  playerEntryPointDelegate: {
    onTriggerCTA: params => handleCta(params.actionType, params.actionParam),
  },
});

On iOS, dismissPlayer() resolves after the player is closed. On Android, it resolves right away, before the player closes, so the player can still be on screen for a moment when your app navigates.

restoreExternalUserId

Available from React Native SDK 2.0.1. The SDK keeps the external user ID across launches until you call logout(). When restoreExternalUserId is true (the default) and you don't pass externalUserId, init restores the previous session's viewer. Set it to false to start this launch anonymous: the SDK logs out a stored viewer during init. restoreExternalUserId is ignored when you pass externalUserId.

On Android, a blank externalUserId counts as not passed, so init restores the stored viewer. On iOS, a blank externalUserId is used as the user ID.

See initialize and logout.

Default player styles

defaultStoryPlayerStyle, defaultMomentsPlayerStyle, and defaultVideosPlayerStyle set the default player style for each content type. See BlazeStoryPlayerStyle and BlazeMomentsPlayerStyle.

From React Native SDK 2.0.1, a playerStyle that you pass per call or per widget is applied on top of the default style, on iOS and Android. A field it omits keeps your default, including fonts, button images, and the iOS CTA icon. Before 2.0.1, an omitted field fell back to the SDK preset. An empty buttons.customActionButtons list still removes the custom action buttons.

On Android, a widget without playerStyle follows the default style, including a widget that mounted before init resolved.

Initialize the SDK

const blazeSDKInit = async () => {
    try {
      const isInitialized = BlazeSDK.isInitialized();
      console.log(isInitialized);
      if (!isInitialized) {
        await BlazeSDK.init(blazeSdkInitOptions);
      }
      setInitialized(true);
    } catch (e) {
      console.log('Init error', e);
      setInitialized(true);
    }
  };

  useEffect(() => {
    blazeSDKInit();

    return () => {
    };
  }, []);

React Native: Android setup

The Experiences SDK Android library is hosted on GitHub Packages. GitHub Packages requires you to authenticate to download an Android library (public or private) hosted there. This requirement might change in future releases.

Step 1: Generate a personal access token for GitHub

Log in to your GitHub account.

Go to Settings > Developer settings > Personal access tokens > Generate new token.

Select the read:packages scope and generate the token. After you generate it, copy your new personal access token. You can't view it again: the only option is to generate a new token.

For more information, see the GitHub personal access token instructions.

Step 2: Store your GitHub personal access token details

In the root of your Android project, open the gradle.properties file.

Add gradle.properties to .gitignore to keep the token private.

Add the following properties:

gprUsr=GITHUB_USERID
gprKey=PERSONAL_ACCESS_TOKEN
  • Replace GITHUB_USERID with your personal or organization GitHub user ID.
  • Replace PERSONAL_ACCESS_TOKEN with the token you generated in Step 1.

React Native: iOS setup

In the iOS environment, follow these steps:

Add the source dependencies to the top of your Podfile.

Sample Podfile:

source 'https://cdn.cocoapods.org/'
source 'https://github.com/WSCSports/blaze-specs-ios.git'


# Resolve react_native_pods.rb with node to allow for hoisting
require Pod::Executable.execute_command('node', ['-p',
  'require.resolve(
    "react-native/scripts/react_native_pods.rb",
    {paths: [process.argv[1]]},
  )', __dir__]).strip

platform :ios, 13
prepare_react_native_project!

# If you are using a `react-native-flipper` your iOS build will fail when `NO_FLIPPER=1` is set.
# because `react-native-flipper` depends on (FlipperKit,...) that will be excluded
#
# To fix this you can also exclude `react-native-flipper` using a `react-native.config.js`
# ```js
# module.exports = {
#   dependencies: {
#     ...(process.env.NO_FLIPPER ? { 'react-native-flipper': { platforms: { ios: null } } } : {}),
# ```
flipper_config = ENV['NO_FLIPPER'] == "1" ? FlipperConfiguration.disabled : FlipperConfiguration.enabled

linkage = ENV['USE_FRAMEWORKS']
if linkage != nil
  Pod::UI.puts "Configuring Pod with #{linkage}ally linked Frameworks".green
  use_frameworks! :linkage => linkage.to_sym
end

target 'WSC RTN Demo Internal' do
  # Comment the next line if you don't want to use dynamic frameworks
  # use_frameworks!
  config = use_native_modules!

  # Flags change depending on the env values.
  flags = get_default_flags()

  use_react_native!(
    :path => config[:reactNativePath],
    # Hermes is now enabled by default. Disable by setting this flag to false.
    :hermes_enabled => flags[:hermes_enabled],
    :fabric_enabled => flags[:fabric_enabled],
    # Enables Flipper.
    #
    # Note that if you have use_frameworks! enabled, Flipper will not work and
    # you should disable the next line.
    :flipper_configuration => flipper_config,
    # An absolute path to your application root.
    :app_path => "#{Pod::Config.instance.installation_root}/.."
  )

  # Pods for WSC RTN Demo Internal

  target 'WSC RTN Demo InternalTests' do
    inherit! :complete
    # Pods for testing
  end

  post_install do |installer|
    # https://github.com/facebook/react-native/blob/main/packages/react-native/scripts/react_native_pods.rb#L197-L202
    react_native_post_install(
      installer,
      config[:reactNativePath],
      :mac_catalyst_enabled => false
    )
    __apply_Xcode_12_5_M1_post_install_workaround(installer)
  end
end

In the project's root folder, run:

bundle install

Go to the ios folder and run:

pod install

Alternatively, you can run:

bundle exec pod install

Run the sample app

The React Native sample app lives in the Blaze-Sample-App folder of the repository.

  1. Clone the repository and open the Blaze-Sample-App folder.

  2. Store your GitHub token in android/gradle.properties, as described in Step 1 and Step 2.

  3. Install the dependencies:

    yarn
  4. In the ios folder, install the pods:

    bundle install
    bundle exec pod install
  5. In src/App.tsx, replace <API_KEY> with your WSC Sports API key.

  6. Start Metro from the Blaze-Sample-App folder:

    yarn start
  7. Build the app from Xcode (open ios/BlazeReactNativeExampleApp.xcworkspace) or from Android Studio (open the android folder).

The home screen shows the Stories, Moments, and Videos widgets. For the Videos widget code, see Videos widgets.

React Native: Expo plugin integration

If your app uses Expo, you can add the Experiences SDK plugin, which integrates the SDK into your app.

Step 1: Generate a personal access token for GitHub

Follow Step 1 of the Android setup.

Step 2: Update your app.json file

Add the plugin as follows:

"plugins":
        [
            ...,
            [
                "@wscsports/blaze-rtn-sdk",
                {
                    "gprUsr": "SetYourGithubUserHere",
                    "gprKey": "SetYourGithubTokenHere"
                }
            ]
        ]

If you don't provide gprUsr and gprKey, the plugin tries to read them from the GPR_USER and GPR_API_KEY environment variables.


Did this page help you?