Hypertune
Search
K
Comment on page

React Native quickstart

1. Install hypertune

Once you have an Expo / React Native application ready, install Hypertune's JavaScript SDK:
npm
yarn
pnpm
npm install hypertune
yarn add hypertune
pnpm add hypertune

2. Set environment variables

Define the following environment variables in your .env file:
HYPERTUNE_TOKEN=token
HYPERTUNE_OUTPUT_FILE_PATH=generated/generated.ts
Replace token with your project token which you can find in the Settings tab of your project.

3. Generate the client

Generate a type-safe client to access your flags by running:
npm
yarn
pnpm
npx hypertune
yarn hypertune
pnpm hypertune

4. Use the client

Add a new file called hypertune.ts that creates and exports a hypertune singleton:
import { initializeHypertune } from "./generated/generated";
const hypertune = initializeHypertune({},
{ token: "your_token" },
);
export default hypertune;
Replace your_token with your project token.
Then define a useHypertune hook in a new file called useHypertune.ts:
import React, { useEffect, useMemo } from "react";
import hypertune from "./hypertune";
export default function useHypertune() {
// Trigger a re-render when flags are updated
const [, setCommitHash] = React.useState<string | null>(
hypertune.getCommitHash(),
);
useEffect(() => {
hypertune.addUpdateListener(setCommitHash);
return () => {
hypertune.removeUpdateListener(setCommitHash);
};
}, []);
// Return the Hypertune root node initialized with the current user
return useMemo(
() =>
hypertune.root({
context: {
user: { id: "test_id", name: "Test", email: "[email protected]" },
},
}),
[],
);
}
Then use the hook in your app:
import useHypertune from "./useHypertune";
export default function App() {
const rootNode = useHypertune();
const exampleFlag = rootNode.exampleFlag().get(/* fallback */ false);
return <Text>Flag: {String(exampleFlag)}</Text>;
}

5. (Optional) Include a build-time logic fallback

If you try accessing your flag on the first app render, you'll get your hardcoded fallback value if the SDK hasn't had a chance to initialize from Hypertune Edge yet. This can result in a UI flicker or layout shift if the flag value changes when the SDK initializes. To avoid this, you can include a snapshot of your flag logic in the generated client as a build-time fallback.
Add the following environment variable to your .env file:
HYPERTUNE_INCLUDE_FALLBACK=true
Then regenerate the client.
The SDK will now instantly initialize from the snapshot first before fetching the latest flag logic from Hypertune Edge. And it will always successfully initialize, even if Hypertune Edge is unreachable. You can keep the snapshot fresh by setting up a webhook to regenerate the client on every Hypertune commit.
If you don't want to include the snapshot but still want to avoid UI flickers and layout shift, you can explicitly wait for initialization from Hypertune Edge with await hypertune.initFromServerIfNeeded() or check for initialization from Hypertune Edge with hypertune.hasInitializedFromServer().

That's it

Now you can update the logic for exampleFlag from the Hypertune UI without updating your code or waiting for a new build, deployment or app release.
To add a new flag, create it in the Hypertune UI then regenerate the client.