Zum Inhalt der Dokumentation springen

React (Vite or Create React App)

Add the CookieBeam loader to index.html in a React single-page app built with Vite or Create React App, and read consent with a hook.

Zuletzt aktualisiert:

A React single-page app has one HTML file that every route is served from. The loader goes in that file's <head>, not in a React component. Read Install in a web framework first for the rules every framework shares.

Add the loader

Build toolFile
Viteindex.html in the project root
Create React Apppublic/index.html

Put the loader first in <head>, after the charset and viewport meta tags:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <!-- 1. CookieBeam first, synchronous -->
    <script src="https://cdn.cookiebeam.com/banner/PUBLIC_BANNER_ID/default/loader.js" data-cfasync="false" data-no-optimize="1" data-no-defer="1" data-no-minify="1" data-noptimize="1" nowprocket="1" data-pagespeed-no-defer="1"></script>
    <!-- 2. Then Google Tag Manager, gtag.js and other tags -->
    <title>My app</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>
  </body>
</html>

Your app's own bundle loads as a module or deferred script, so it runs after the loader. Don't add the CookieBeam tag from React code (for example with document.createElement in an effect): by that point the page has already run whatever came before it.

Route changes

React Router and other client-side routers change the URL without reloading the page. The loader runs once, and the runtime, banner and consent stay in place across routes.

Wrap the hasConsent and onConsentChange helpers from Read consent in your code:

// src/use-consent.ts
import { useEffect, useState } from 'react';
import { hasConsent, onConsentChange } from './consent';

export function useConsent(category: string): boolean {
  const [granted, setGranted] = useState(false);

  useEffect(() => {
    const update = () => setGranted(hasConsent(category));
    update();
    return onConsentChange(update);
  }, [category]);

  return granted;
}
import { useConsent } from './use-consent';

export function RequireConsent({
  category,
  children,
}: {
  category: string;
  children: React.ReactNode;
}) {
  return useConsent(category) ? <>{children}</> : null;
}

// <RequireConsent category="marketing"><VideoEmbed /></RequireConsent>

To keep a vendor's code out of the initial bundle, load the gated component with React.lazy and render it only inside RequireConsent.

For the cookie settings link, use a plain button with data-cb="show-preferencesModal", not your router's <Link>. See Add a cookie settings link.

Content security policy

If your server sends a Content-Security-Policy header, see Content security policy. A static index.html has no per-request nonce, so either list the CookieBeam hosts in the policy or have your server add the nonce to the tag.

Check the installation

  1. Build the app (vite build or npm run build) and serve the output, or check the deployed site. View the page source and confirm the loader is the first script in <head>.
  2. Add localhost or your preview hostname under Banner domains and publish, or the banner won't appear there.
  3. Navigate between routes. The Network panel shows a single loader.js request.
  4. Reject, then accept, a category and confirm gated components follow the choice without a reload.

Then work through Verify your installation.