SDKs
React, Vue & Svelte
There's no React package, and no Vue or Svelte one. A binding is twenty lines over two methods the client already has. Copy the one you need; then it's yours.
A package per framework turns those twenty lines into a version matrix: releases owed on another project's schedule, and anyone on a framework we haven't packaged told to wait.
Each one assumes a client built once, at module scope:
import { FeaturekitClient } from "featurekit-client";
export const featurekit = new FeaturekitClient({
clientKey: import.meta.env.VITE_FEATUREKIT_CLIENT_KEY,
baseUrl: "https://flags.acme.com",
context: { key: user.id, plan: user.plan },
stream: true,
});
import { useCallback, useMemo, useSyncExternalStore } from "react";
import { featurekit } from "./featurekit";
export function useFlag<T>(flagKey: string, defaultValue: T): T {
const subscribe = useCallback((onChange: () => void) => {
// `state` matters too: identify() drops the previous user's values before the network answers.
const offChange = featurekit.on("change", onChange);
const offState = featurekit.on("state", onChange);
return () => { offChange(); offState(); };
}, []);
// `detail` keeps the same object until a real change, which React needs to avoid re-render loops.
const getSnapshot = useMemo(() => () => featurekit.detail(flagKey), [flagKey]);
const detail = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
return detail === undefined ? defaultValue : featurekit.get(flagKey, defaultValue);
}
// in a component
const showNew = useFlag("new-checkout", false);
import { onScopeDispose, shallowRef, type Ref } from "vue";
import { featurekit } from "./featurekit";
export function useFlag<T>(flagKey: string, defaultValue: T): Ref<T> {
const read = (): T => featurekit.get(flagKey, defaultValue);
const value = shallowRef(read());
const update = () => { value.value = read(); };
const offChange = featurekit.on("change", update);
const offState = featurekit.on("state", update);
onScopeDispose(() => { offChange(); offState(); });
return value;
}
import { readable, type Readable } from "svelte/store";
import { featurekit } from "./featurekit";
export function flag<T>(flagKey: string, defaultValue: T): Readable<T> {
const read = (): T => featurekit.get(flagKey, defaultValue);
return readable(read(), (set) => {
const update = () => set(read());
const offChange = featurekit.on("change", update);
const offState = featurekit.on("state", update);
return () => { offChange(); offState(); };
});
}
// in a component: {#if $newCheckout} … with const newCheckout = flag("new-checkout", false)
featurekit.on("change", () => render());
featurekit.on("state", () => render());
await featurekit.ready();
function render() {
document.body.classList.toggle("new-checkout", featurekit.get("new-checkout", false));
}
Three things every adapter must get right
- Listen to
stateas well aschange. When the user changes, the old values are dropped immediately and announced as a state change. Listening only tochangeshows the previous user's flags until the network answers. - Let
get()own the default. It already handles a missing flag and a value of the wrong type. Reimplementing that check is how two parts of an app end up disagreeing. - Never build a client per component. One instance, built once. Reads are synchronous so a component can call one during render without a promise or a suspense boundary.
React Native
Use the React hook as written. For live updates, give the client Expo's streaming fetch; see the client SDK. The Expo example shows it in a working app.