React SDK reference
React 19+ integration for inert consent-gated markup, synchronous returning-visitor acceleration, and reactive consent reads.
npm install --save-exact @1consent/reactFor Next.js, install @1consent/nextjs instead and import the same React components from @1consent/nextjs.
OneConsentProvider
function OneConsentProvider(props: OneConsentProviderProps): ReactNodeRenders 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
| Option | Description | Default |
|---|---|---|
appId?: string | Public 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?: string | CSP nonce for emitted scripts. | — |
cmpSrc?: string | Override the CMP embed URL derived from appId. | — |
environment?: "local" | "develop" | "production" | 1Consent service environment. | production |
loadCmp?: boolean | Set false when another integration loads the CMP embed. | true |
children?: ReactNode | Application children. | — |
Example
<OneConsentProvider appId="app_demo" fastPathVersions={fastPathVersions}>
{children}
</OneConsentProvider>OneConsent
function OneConsent(props: OneConsentProps): ReactNodeSerializes 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
| Option | Description | Default |
|---|---|---|
service: RegisteredServiceName | Service name, tag, or ID; mutually exclusive with category. | — |
category: RegisteredCategoryName | Category name or ID; mutually exclusive with service. | — |
nonce?: string | CSP nonce for the inline resolve call. | — |
once?: boolean | Promote a matching global side-effect block at most once per page load. | false |
children?: ReactNode | Plain 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): booleanReturns 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
| Option | Description | Default |
|---|---|---|
key: string | Service or category name, tag, or ID. | — |
Example
const analyticsAllowed = useConsent("Google Analytics");Public types
OneConsentProps | OneConsentProviderProps | OneConsentEnvironment | OneConsentServiceMap | OneConsentCategoryMap | RegisteredServiceName | RegisteredCategoryNameExported 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:
| Member | Type | Purpose |
|---|---|---|
fastPathVersions | Readonly<Record<string, string>> | Framework ID to structural build stamp map passed to OneConsentProvider. |
OneConsentServiceName / OneConsentCategoryName | string literal unions | Names and IDs aggregated across all published frameworks for the app. |
serviceIds / categoryIds | Record<generated name, string> | Stable generated name-to-ID maps for customer application code. |