React Integration
Add consent management with the @1consent/react package. It is pure React — it works in Next.js (App Router and Pages Router), Vite SPAs, Remix/React Router, and Astro islands.
How it works: wrap any third-party markup in <OneConsent>. It renders as an inert <template> that cannot execute — no scripts run, no images load, no iframes connect. A tiny inline engine (installed by the provider) resolves it:
- Returning visitors with valid stored consent can activate a complete gate when its parse-time resolver runs, using the selected framework and consent epoch from synchronous hosted configuration. Missing, expired or stale consent stays inert until the CMP establishes current permission.
- First-time visitors see the consent banner; gated content is released the moment they decide.
- The server never reads consent. Your HTML is byte-identical for every visitor, so static rendering, ISR, and CDN caching are fully preserved.
This preserves consent and script dependency order; it does not guarantee execution before first paint. Streamed HTML may paint before a later gate arrives, and external downloads can finish after paint. Blocked or unavailable hosted configuration prevents early activation. SPA gates activate after mounting through the inline engine's observer; they have no initial-paint promise.
For an overview and AI-assisted setup, see React integration. Next.js users can follow the dedicated App Router guide.
Installation
The React integration, Next.js adapter and scoped CLI are available as a public beta. Use the explicit @beta tag in the installation commands.
For a React application:
npm install @1consent/react@betaFor a Next.js application, you can use the adapter instead. It exposes the same React API and optional server helpers:
npm install @1consent/nextjs@betaWhen using the adapter, import the React components and hooks below from @1consent/nextjs instead of @1consent/react.
Setup
Generate typed service names
Run this command from your project directory, replacing YOUR_APP_ID with the public App ID from your Dashboard. Type generation reads the app's published frameworks:
npx @1consent/cli@beta typegen --app YOUR_APP_IDThe CLI defaults to the production environment. For a development or local app, add --env develop or --env local respectively; use the matching environment prop on OneConsentProvider.
This writes src/1consent.gen.ts — or 1consent.gen.ts at the project root if you have no src/ directory. Point --out anywhere you like to override it. The file contains:
OneConsentServiceName/OneConsentCategoryNameunions — theserviceandcategoryprops autocomplete, and renaming a service in the dashboard becomes a build-time type error pointing at the exact JSX usage,fastPathVersions— a framework-ID → structural-version map, so the inline engine can trust the exact framework selected for each visitor. Service and category types are unioned across every published framework.
Re-run after publishing changes to services or categories. In CI, check for stale types without rewriting the file:
npx @1consent/cli@beta typegen --app YOUR_APP_ID --checkAdd the Provider
Wrap your application in your root layout:
import { OneConsentProvider } from '@1consent/react';
import { fastPathVersions } from '@/1consent.gen';
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
<OneConsentProvider
appId="YOUR_APP_ID"
fastPathVersions={fastPathVersions}
>
{children}
</OneConsentProvider>
</body>
</html>
);
}The provider renders the inline consent engine and loads the CMP (banner + preference center). appId falls back to the NEXT_PUBLIC_1CONSENT_APP_ID environment variable.
@/1consent.gen is your project's own path alias — in Next.js and Vite, @/ resolves to src/, so it matches the file 1c typegen writes to src/1consent.gen.ts. If your project has no @/ alias (or no src/ directory), import the generated file with a relative path instead.
Omitting fastPathVersions is safe — everything still works, but returning visitors lose the parse-time fast path until the CMP loads. The synchronous hosted config selects the visitor's live framework and consent epoch; the generated map only supplies that framework's structural build stamp. A publish that resurfaces consent therefore takes effect without a customer rebuild.
Wrap third-party scripts
Wrap anything that requires consent — scripts, pixels, iframes, embeds:
import { OneConsent } from '@1consent/react';
<OneConsent service="Google Analytics" once>
<script src="https://www.googletagmanager.com/gtag/js?id=G-XXXXX" />
<script>{`window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXX');`}</script>
</OneConsent>Or gate by category:
<OneConsent category="Marketing">
<iframe title="Optional video" src="https://www.youtube.com/embed/dQw4w9WgXcQ" />
</OneConsent>The service value matches the service's name, tag, or id as configured in your dashboard. Children must be plain HTML elements (they are serialized into inert HTML — React components can't be gated this way; gate the component's own output instead).
Remount semantics — when to use once: if a <OneConsent> unmounts and remounts (SPA navigation, conditional render), its content is promoted again. That is what you want for visual content and UI-rendering scripts — the embed was destroyed with the old mount and must be restored. It is not what you want for global side-effect scripts like the gtag loader above, which would execute twice. Add once to those: the block then runs at most once per page load, no matter how often it remounts. Never put once on visual embeds.
Place <OneConsent> inside <body>, after the provider. Its wrapper is a <div>, which is invalid inside <head> and can cause browser relocation and hydration mismatches. A complete gate can activate during parsing when valid consent and the required hosted configuration are available; its position, streamed arrival and external dependencies still determine execution timing.
Using consent in components
Access consent state in client components with the useConsent hook:
'use client';
import { useConsent } from '@1consent/react';
export function PersonalizedBanner() {
const granted = useConsent('Google Analytics'); // service or category
if (!granted) return null;
return <TrackedContent />;
}The hook returns false until consent is known and re-renders on consent changes.
Provider configuration
| Prop | Type | Default | Description |
|---|---|---|---|
appId | string | NEXT_PUBLIC_1CONSENT_APP_ID | Your 1Consent App ID |
fastPathVersions | Readonly<Record<string, string>> | — | Framework-ID → build-stamp map from 1consent.gen.ts; enables the parse-time fast path |
nonce | string | — | CSP nonce for the inline engine script |
cmpSrc | string | derived from appId | Override the CMP embed script URL |
environment | local | develop | production | production | Select a 1Consent service environment (internal setups only) |
loadCmp | boolean | true | Set false when the CMP embed is added elsewhere (e.g. GTM) |
<OneConsent> props: exactly one of service / category, plus optional nonce (strict-CSP sites) and once (promote at most once per page load — for side-effect scripts that must not re-execute on remount; identical once blocks share the guard).
Consent semantics
- Gated content only ever transitions inert → live. When a visitor revokes previously granted consent, the CMP reloads the page — scripts cannot be "un-run".
- If the consent configuration changes structurally (services added/removed), stored consent no longer matches
structuralVersionand the engine defers to the CMP — visitors are never given scripts they didn't consent to. - Crawlers and bots (no stored consent) never see gated scripts execute.
Best practices
- Commit
1consent.gen.ts(or regenerate it in your build step) and add--checkto CI. - Use environment variables for the App ID to keep it configurable per deployment.
- Wrap, don't fork: prefer
<OneConsent>over conditional rendering withuseConsent— wrapped content gets the parse-time fast path; hook-gated content always waits for hydration.