@vercel/flags/react

If you are using React, you can use the FlagValues and FlagDefinitions components. These abstract you from needing to manually render script tags. These components handle setting the correct data attributes and escaping any data to prevent XSS.

FlagValues

This component is a convenience method to render the script tag which is used by the Flags Explorer and Web Analytics to learn about the values your feature flags evaluated to.

Pass flag data into the FlagValues component with the values prop.

PropTypeDescription
valuesFlagValuesTypeThe feature flag values to expose to the Vercel Toolbar
app/page.tsx
import { FlagValues } from '@vercel/flags/react';
export function Page() {
return (
<div>
{/* Some other content */}
<FlagValues values={{ exampleFlag: true }} />
</div>
);
}

To keep your flags confidential, encrypt the input:

app/page.tsx
import { encrypt } from '@vercel/flags';
import { FlagValues, type FlagValuesType } from '@vercel/flags/react';
async function ConfidentialFlagValues({ values }: { values: FlagValuesType }) {
const encryptedFlagValues = await encrypt(values);
return <FlagValues values={encryptedFlagValues} />;
}
export function Page() {
const values = { exampleFlag: true };
return (
<div>
{/* Some other content */}
<Suspense fallback={null}>
<ConfidentialFlagValues values={values} />
</Suspense>
</div>
);
}

FlagDefinitions

This component is a convenience method to render the script tag which is used by the Flags Explorer to learn metadata about your feature flags, like the description.

Pass flag data into the FlagDefinitions component with the definitions prop.

PropTypeDescription
definitionsFlagDefinitionsTypeThe feature flag definitions to expose to the Vercel Toolbar
app/page.tsx
import { FlagDefinitions } from '@vercel/flags/react';
export function Page() {
const flagDefinitions = {
exampleFlag: {
options: [{ value: false }, { value: true }],
origin: 'https://example.com/flag/exampleFlag',
description: 'This is an example flag.',
},
};
return (
<div>
{/* Some other content */}
<FlagDefinitions definitions={flagDefinitions} />
</div>
);
}

To keep your flags confidential, encrypt the input:

app/page.tsx
import { encrypt, type FlagDefinitionsType } from '@vercel/flags';
import { FlagDefinitions } from '@vercel/flags/react';
async function ConfidentialFlagDefinitions({
definitions,
}: {
definitions: FlagDefinitionsType;
}) {
const encryptedFlagDefinitions = await encrypt(definitions);
return <FlagDefinitions values={encryptedFlagDefinitions} />;
}
export function Page() {
const flagDefinitions = {
exampleFlag: {
options: [{ value: false }, { value: true }],
origin: 'https://example.com/flag/exampleFlag',
description: 'This is an example flag.',
},
};
return (
<div>
{/* Some other content */}
<Suspense fallback={null}>
<ConfidentialFlagDefinitions values={flagDefinitions} />
</Suspense>
</div>
);
}