React
Install
npm install @flipfeatureflag/react @flipfeatureflag/js
Provider
import { FlipFlagProvider } from "@flipfeatureflag/react";
function App() {
return (
<FlipFlagProvider
config={{
url: "https://api.example.com",
sdkKey: "YOUR_SDK_KEY",
env: "prod",
}}
>
<YourRoutes />
</FlipFlagProvider>
);
}
Hooks
import { useFlag, useVariant, useFlagsStatus, useUpdateContext } from "@flipfeatureflag/react";
function Checkout() {
const { enabled, value } = useFlag("new_checkout");
const variant = useVariant("new_checkout");
const status = useFlagsStatus();
const updateContext = useUpdateContext();
if (!status.ready) {
return null;
}
return enabled ? <NewCheckout /> : <OldCheckout />;
}
Updating context after login
import { useUpdateContext } from "@flipfeatureflag/react";
function AuthListener({ userId }) {
const updateContext = useUpdateContext();
useEffect(() => {
if (userId) {
updateContext({ userId });
}
}, [userId, updateContext]);
return null;
}
useFlag return value
useFlag returns { value, enabled }:
valueis the current flag value (boolean/string/number/object).enabledistruewhen the flag exists in the backend response andreason !== "disabled".- If the flag is missing from the response,
enabledisfalse. - If
reasonisdefault,enabledis stilltrueas long as the flag was returned.