Evaluate feature flags

Turn features on and pick variants per person, without an app release.

Voidhash evaluates feature flags for the current person, so one flag can be on for some people and off for others. You create flags and set their rollout in Studio; the SDK reads the result.

A build that uses a test key gets each flag's test configuration, which is the same as production's until you change it in Studio. See Environments.

Evaluate flags

Pass the keys you need. The hook compares keys by value, in any order, so an inline array doesn't cause extra requests:

import { voidhash } from "../src/lib/voidhash";

export function HomeScreen() {
  const { isEnabled, getVariantValue, isLoading } = voidhash.useFeatureFlags([
    "new-onboarding",
    "pricing-test",
  ]);

  if (isLoading) return null;

  return (
    <Home
      newOnboarding={isEnabled("new-onboarding")}
      pricingVariant={getVariantValue("pricing-test")}
    />
  );
}

Omit the keys to evaluate every flag.

ValueMeaning
isEnabled(key)Whether the flag is on. While it has no value yet, its default, or false.
getVariantValue(key)The value of the variant the person got, such as "control". null while loading, and when off.
getVariant(key){ enabled, variantKey, payload }, or null while loading or for an unknown flag.
data.flagsEvery evaluated flag: key, enabled, variantKey, payload.
isLoading, error, refetchRequest state, and a way to evaluate again.

Match on the variant's value with getVariantValue(). variantKey is an id Voidhash generates for the variant, not its value. payload is untyped JSON, or null; validate it before use.

getVariantValue() is null both while the flag is loading and when it is off, so check isLoading to tell them apart:

const { getVariantValue, isLoading } = voidhash.useFeatureFlags(["onboarding-test"]);
// `undefined` while loading; once loaded, the person's variant, or "control" when the flag is off.
const variant = isLoading ? undefined : (getVariantValue("onboarding-test") ?? "control");

Outside React, client.getFeatureFlags(keys?) resolves to { flags, isStale }, and client.getVariantValue(key) to the variant's value or null.

Defaults and caching

A flag without a value yet reads as off, unless you give it a default. Write your code so that off is the safe behavior, and set a default only for a feature that must stay on when Voidhash can't be reached:

import { createVoidhashClient } from "@voidhash/react-native";

export const voidhash = createVoidhashClient("vh_pk_...", {
  featureFlags: {
    defaults: { "new-checkout": true },
    maxWaitMs: 6000,
  },
});

With nothing cached, a read waits up to maxWaitMs (2 seconds by default) for Voidhash, then answers without the flag; the hooks update when the answer arrives. A request gives up after 10 seconds, so a longer wait ends there.

Results are cached on the device and served from cache for 5 minutes. After that, a read answers from cache while a refresh runs, so flags keep working offline. The SDK also evaluates again at launch and on foreground. A change in Studio reaches the app at the next refresh.

Identity

Flags are evaluated per person. After identify(), signOut() or reset(), mounted hooks evaluate again for the new person. Rollouts bucket by person, so the same person gets the same answer on every device once identified.

Don't gate paid access on a flag. Use perks for that.

Next steps

Something wrong or missing on this page? Tell us by email or on Discord.