React quickstart
1. Install hypertune
hypertuneOnce you have a React application ready, install Hypertune's JavaScript SDK:
npm install hypertuneyarn add hypertunepnpm add hypertune2. Set environment variables
Define the following environment variables in your .env file:
VITE_HYPERTUNE_TOKEN=token
HYPERTUNE_FRAMEWORK=react
HYPERTUNE_OUTPUT_DIRECTORY_PATH=src/generatedReplace token with your main 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:
npx hypertuneyarn hypertunepnpm hypertune4. Use the client
Create a new <AppHypertuneProvider> component that wraps the generated <HypertuneProvider> component:
import { HypertuneProvider } from '../generated/hypertune.react'
export default function AppHypertuneProvider({
children,
}: {
children: React.ReactNode
}) {
return (
<HypertuneProvider
createSourceOptions={{
token: import.meta.env.VITE_HYPERTUNE_TOKEN!,
}}
rootArgs={{
context: {
environment:
process.env.NODE_ENV === 'development'
? 'development'
: 'production',
user: {
id: 'e23cc9a8-0287-40aa-8500-6802df91e56a',
name: 'Example User',
email: '[email protected]',
},
},
}}
>
{children}
</HypertuneProvider>
)
}Wrap your app with the <AppHypertuneProvider> component:
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.tsx'
import AppHypertuneProvider from './components/AppHypertuneProvider.tsx'
import './index.css'
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<AppHypertuneProvider>
<App />
</AppHypertuneProvider>
</React.StrictMode>
)Then use the generated useHypertune hook:
import { useHypertune } from '../generated/hypertune.react'
export default function ClientComponent() {
const hypertune = useHypertune()
const exampleFlag = hypertune.exampleFlag({ fallback: false })
return <div>Example Flag: {String(exampleFlag)}</div>
}If you have a Content Security Policy, add the following to your connect-src directive: https://edge.hypertune.com https://gcp.fasthorse.workers.dev. This enables reporting of flag evaluations, experiment exposures, and analytics events.
5. (Optional) Add the Hypertune Toolbar
The Hypertune Toolbar lets you view and override feature flags directly in your frontend. Follow the guide to add it to your app.
6. (Optional) Include a build-time snapshot
If you try accessing a flag immediately after the app loads, you'll get your hardcoded fallback value if the SDK hasn't initialized from Hypertune Edge yet. This can result in layout shift or a flicker 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 at build time. The SDK will instantly initialize from the snapshot first before fetching the latest flag logic from Hypertune Edge.
Add the following environment variable to your .env file:
HYPERTUNE_INCLUDE_INIT_DATA=trueThen regenerate the client.
You can keep the snapshot fresh by setting up a webhook to regenerate the client and redeploy your app on every Hypertune commit. In this case, you don't need to initialize from Hypertune Edge at all.
Next steps
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.
Last updated