Skip to content

FlagDiligence docs

React SDK

A provider, hooks and a component for your front end. Requires React 16.8 or newer.

Install

npm install @weborlabs/flagdiligence-react

Published to the GitHub Packages registry like the Node.js SDK. See the install note on the Node.js SDK page.

Add the provider

import { FlagDiligenceProvider } from '@weborlabs/flagdiligence-react';

<FlagDiligenceProvider
  sdkKey="sdk-client_your_key"
  baseUrl="https://your-flagdiligence-host"
  mode="streaming"
  user={user}
>
  <App />
</FlagDiligenceProvider>

Use the client key here. You can pass the same settings as one config object if you prefer. Build the user object once, after you know who the user is, and pass the same object on each render.

Options

OptionDescription
sdkKey, userRequired. The client key and the user to evaluate for.
baseUrlYour FlagDiligence server address.
mode"streaming", "polling" or "static". Set it deliberately; the default is "static".
pollingIntervalMilliseconds between checks in polling mode (at least 10000).
timeoutRequest timeout in milliseconds (1000 to 30000).
bootstrapStarting values to use before the first fetch finishes, useful for server rendering and avoiding a flash of defaults.
onError, onUpdateCallbacks for failed requests and for new flag values.

Hooks

import {
  useBooleanFlag, useStringFlag, useNumberFlag, useJsonFlag, useFlag,
} from '@weborlabs/flagdiligence-react';

function Banner() {
  const { enabled, isLoading } = useBooleanFlag('promo_banner', false);
  const { value: theme }       = useStringFlag('ui_theme', 'light');
  const { value: limit }       = useNumberFlag('max_items', 10);

  if (isLoading) return null;
  return enabled ? <Promo theme={theme} limit={limit} /> : null;
}

Boolean hooks return { enabled, isLoading }. The string, number, JSON and generic useFlag hooks return { value, isLoading }.

The Feature component

import { Feature } from '@weborlabs/flagdiligence-react';

<Feature flag="new_checkout_flow" fallback={<OldCheckout />}>
  <NewCheckout />
</Feature>

While flags are loading, the fallback is shown. There is also withFeatureFlag to wrap a component, and useFlagDiligence to read every flag, the loading state and any error.

Server rendering

You cannot read window or navigator on the server. Detect browser and device details in the browser after the page loads, or parse the User-Agent header on the server, and pass the result as custom attributes. Use bootstrap to render the first paint with known values.

← Node.js SDKUser guide →

Need help with FlagDiligence?

Open the app, or tell us what you are trying to do.