1Consent Docs

React SDK reference

React 19+ integration for inert consent-gated markup, synchronous returning-visitor acceleration, and reactive consent reads.

npm install --save-exact @1consent/react

For Next.js, install @1consent/nextjs instead and import the same React components from @1consent/nextjs.

OneConsentProvider

function OneConsentProvider(props: OneConsentProviderProps): ReactNode

Renders the synchronous CMP embed configuration, the inline React fast path, and application children without reading consent on the server. On a client-only mount, where React never runs a rendered script, it inserts the CMP embed (unless the page already loads it) and starts the fast path once the CMP configuration is available.

Parameters and props

OptionDescriptionDefault
appId?: stringPublic site ID; falls back to NEXT_PUBLIC_1CONSENT_APP_ID.—
fastPathVersions?: Readonly<Record<string, string>>Framework ID to structural build stamp map generated by 1c typegen.—
nonce?: stringCSP nonce for emitted scripts.—
cmpSrc?: stringOverride the CMP embed URL derived from appId.—
environment?: "local" | "develop" | "production"1Consent service environment.production
loadCmp?: booleanSet false when another integration loads the CMP embed.true
children?: ReactNodeApplication children.—

Example

<OneConsentProvider appId="app_demo" fastPathVersions={fastPathVersions}>
  {children}
</OneConsentProvider>

OneConsent

function OneConsent(props: OneConsentProps): ReactNode

Serializes plain HTML children into an inert template and promotes them when the selected service or category has consent. Exactly one gating key is required.

Parameters and props

OptionDescriptionDefault
service: RegisteredServiceNameService name, tag, or ID; mutually exclusive with category.—
category: RegisteredCategoryNameCategory name or ID; mutually exclusive with service.—
nonce?: stringCSP nonce for the inline resolve call.—
once?: booleanPromote a matching global side-effect block at most once per page load.false
children?: ReactNodePlain HTML elements to serialize; React components are unsupported.—

Example

<OneConsent service="Google Analytics" once>
  <script src="https://example.com/analytics.js" />
</OneConsent>

useConsent

function useConsent(key: string): boolean

Returns the saved consent decision for a service or category key and re-renders when it changes. Returns false until consent is known: once the CMP has loaded, with or without OneConsentProvider, or from the first client render for a stored choice when the provider has the generated fastPathVersions. A choice that is toggled but not saved does not count. The React guide lists the limits under IAB TCF and US opt-out banners. Package versions 0.1.0 and earlier report consent only with the provider and a matching fastPathVersions entry.

Parameters and props

OptionDescriptionDefault
key: stringService or category name, tag, or ID.—

Example

const analyticsAllowed = useConsent("Google Analytics");

Public types

OneConsentProps | OneConsentProviderProps | OneConsentEnvironment | OneConsentServiceMap | OneConsentCategoryMap | RegisteredServiceName | RegisteredCategoryName

Exported prop and declaration-merging types. Generated service/category modules augment the empty maps to make registered names autocomplete and type-check.

Generated module from 1c typegen

Commit the generated module or run 1c typegen --app <shortId> --check in CI. It augments the public React maps and exports:

MemberTypePurpose
fastPathVersionsReadonly<Record<string, string>>Framework ID to structural build stamp map passed to OneConsentProvider.
OneConsentServiceName / OneConsentCategoryNamestring literal unionsNames and IDs aggregated across all published frameworks for the app.
serviceIds / categoryIdsRecord<generated name, string>Stable generated name-to-ID maps for customer application code.

On this page