This guide is for React apps that render in the browser only, such as Vite or Create React App. If you render on the server with Next.js, use the Next.js guide instead: it loads flags on the server so nothing flickers.
Install
npm i @flaggish/sdk@betaCopy the client key from Settings › API keys in the dashboard. It’s safe in the browser: it can read flags, never change them.
Add the provider
Wrap your app in FlaggishProvider:
import { FlaggishProvider } from '@flaggish/sdk/react';
export const App = () => (
<FlaggishProvider clientKey="your-client-key" defaults={{ 'new-checkout': false }}>
<Page />
</FlaggishProvider>
);The provider fetches your flags once, when it mounts. It also creates the anonymous flaggish_id cookie if the visitor doesn’t have one, so they get the same answer in a rollout on every visit.
defaults are used until the flags load, for flags that don’t exist, and if Flaggish can’t be reached. The provider takes every option in SDK options, plus userId, email and groupIds.
Check a flag
import { useFlag } from '@flaggish/sdk/react';
export const Checkout = () => {
const enabled = useFlag('new-checkout');
return enabled ? <NewCheckout /> : <OldCheckout />;
};useFlag(key, fallback?) returns true or false. It reads the flag from the config, then your defaults, then fallback, which is false if you leave it out. Outside a provider it returns fallback.
The loading state
Until the flags load, useFlag returns your defaults. If showing the old version for a moment would look wrong, wait for the flags with useFlags:
import { useFlag, useFlags } from '@flaggish/sdk/react';
export const Page = () => {
const enabled = useFlag('new-checkout');
const { isLoading } = useFlags();
if (isLoading) return null;
return enabled ? <NewCheckout /> : <OldCheckout />;
};useFlags() returns { flags, isLoading, error }:
flagsis every flag as an object of booleans, with yourdefaultsmerged in.isLoadingistrueuntil the first fetch finishes, whether it worked or not.erroris effectively alwaysnull. The SDK never rejects: if the fetch fails, you get your defaults instead.
Signed-in users
Pass the user’s ID as userId. It’s used in place of the anonymous ID, so a signed-in person gets the same answer on every device and browser:
import { FlaggishProvider } from '@flaggish/sdk/react';
export const App = ({ user }: { user: { id: string } | null }) => (
<FlaggishProvider clientKey="your-client-key" userId={user?.id ?? null}>
<Page />
</FlaggishProvider>
);userId can be null for signed-out visitors, who then use the anonymous ID. The ID stays in the browser: it isn’t sent to Flaggish.
Someone who browses anonymously and then signs in switches from their anonymous ID to their user ID, so in a partial rollout their answer can change at sign-in.
Groups
Groups give a flag to specific people first. In a browser-only app, company domains work: pass the signed-in user’s email, and anyone whose email is at one of a group’s domains gets the flag.
import { FlaggishProvider } from '@flaggish/sdk/react';
import { useUser } from './auth'; // your auth hook
import { Page } from './Page';
export const App = () => {
const user = useUser(); // from your auth library; null when signed out
return (
<FlaggishProvider clientKey="your-client-key" userId={user?.id ?? null} email={user?.email ?? null}>
<Page />
</FlaggishProvider>
);
};Lists (“Specific people”) can’t be checked in the browser, because the people in a list never reach a browser. A browser-only app has no server to hold the secret key, so a list group counts as “not in the group” and people fall to the percentage. The SDK doesn’t warn in the browser, because the provider never takes a secret key. To use lists, check them on a server with your secret key and pass the result as the groupIds prop, or use Next.js, where FlaggishServerProvider does it for you. Never put the secret key in browser code.
The email is only used to match groups, inside your app. It isn’t sent to Flaggish, and it never decides a percentage rollout.
When changes show up
The provider loads flags once per page load. A page that’s already open keeps the answers it loaded; after you change a flag in the dashboard, people see it the next time they load the page.
Environment
The provider reads Production flags when process.env.NODE_ENV is production in your build, and Development flags otherwise, including when your bundler doesn’t provide process.env at all. To be sure which you get, pass env="production" or env="development":
import { FlaggishProvider } from '@flaggish/sdk/react';
import { Page } from './Page';
export const App = () => (
<FlaggishProvider clientKey="your-client-key" env="production">
<Page />
</FlaggishProvider>
);