React.js integration

Cookie consent built for React applications.

Add CookieHub to your React app with react-cookiehub, keep consent state consistent across client-side navigation and control when analytics, marketing and embedded services are allowed to run.

  • React package
  • SPA-friendly consent state
  • Consent-controlled scripts
Built for React

One consent state across your React app.

React apps don’t load like traditional websites. Components mount as visitors move through the app, routes change without a full page refresh and scripts can be loaded dynamically — so tracking can fire before a visitor has made a choice if consent isn’t handled in one place.

CookieHub gives your React app a central consent layer. Initialize it once at the root of your app with react-cookiehub, then load analytics, marketing and other non-essential tools only when the visitor has allowed the relevant consent category.

  • Consent-first script loading

    Only initialize analytics, marketing or other non-essential tools when the relevant category is allowed.

  • SPA-friendly consent

    Initialize CookieHub centrally and keep visitor consent consistent across client-side route changes.

  • React-specific controls

    Use the package’s React components and events rather than scattering consent checks throughout the app.

React package

Consent controls designed for React.

react-cookiehub is CookieHub’s package for React and Next.js apps. It loads CookieHub from the root of your app and gives you components and events for anything that should wait for consent:

  • CookieHub.initialize() loads CookieHub with your domain code
  • CookieHubScript loads an external or inline script in the consent category you set
  • CookieHubGA and CookieHubPixel load Google Analytics and Meta Pixel under the analytics and marketing categories
  • CookieHubYoutube embeds YouTube videos through a youtube-nocookie URL
  • onAllow, onRevoke and other events let your app respond when consent changes

The package guide has the full reference, including setup for the Next.js App Router and Pages Router.

View package guide
What CookieHub handles in React

Cookie consent tools for your React app.

  • Consent-first script loading

    CookieHubScript adds a script in the consent category you choose — such as analytics, marketing or preferences — so it only runs once the visitor allows that category.

  • Stable consent across route changes

    CookieHub is loaded once from your app root. Client-side navigation doesn’t reload the page, so the visitor’s choice stays in effect as routes change.

  • Google Analytics component

    CookieHubGA loads Google Analytics with your tracking ID under the analytics category, so it waits for analytics consent.

  • Meta Pixel component

    CookieHubPixel loads the Meta (Facebook) Pixel with your pixel ID under the marketing category, so it waits for marketing consent.

  • Embedded media control

    CookieHubYoutube embeds a YouTube video from its URL or video ID through a youtube-nocookie URL, which doesn’t set YouTube’s tracking cookies.

  • Consent events and API

    Events such as onAllow, onRevoke and onStatusChange, and methods such as hasConsented() and openSettings(), let your app respond to consent without a page refresh.

How it works

Add CookieHub to React in a few steps.

These steps follow the react-cookiehub package guide. Building with Next.js? CookieHub is initialized client-side there — see the notes below.

  1. 01

    Install the package

    Add react-cookiehub to your React project.

    npm install react-cookiehub
  2. 02

    Initialize CookieHub with your domain code

    Each domain you add in the CookieHub dashboard is assigned a unique 8-character domain code. Import CookieHub from react-cookiehub and call CookieHub.initialize() with that code in your root component, such as App.js, so it runs for the whole app.

    CookieHub.initialize('YOUR-DOMAIN-CODE')
  3. 03

    Load non-essential services through CookieHub

    Add scripts with CookieHubScript and their consent category, or use CookieHubGA, CookieHubPixel and CookieHubYoutube. For other tracking packages, use onInitialise to handle existing consent and onAllow when consent is newly granted.

    category="analytics"
  4. 04

    Test consent across route changes

    Check that tracking doesn’t run before consent, then move between routes and change your choice in the settings dialog. Set debug: true in the options for more detail in the console.

    debug: true
  • Using the Next.js App Router?

    CookieHub relies on browser APIs such as window, so initialize it client-side: create a client component that calls CookieHub.initialize() inside useEffect, and render it in your root layout.

    Next.js App Router setup
  • Using the Next.js Pages Router?

    Call CookieHub.initialize() inside useEffect in your _app file, so CookieHub doesn’t run on the server. As Next.js can affect script load order, the package guide recommends CookieHub’s script components.

    Next.js Pages Router setup
Scripts & routing

Keep consent intact as your app changes routes.

In a single-page app, React mounts components and loads services as visitors navigate — without a full page refresh. react-cookiehub adds CookieHub once, from your app root, so the visitor’s consent decision stays available to every route and component.

  1. Your root component initializes CookieHub
  2. CookieHub holds the visitor’s consent state
  3. Routes change without a page reload
  4. Scripts run only in allowed categories
Consent-controlled scripts

Load third-party scripts only after consent.

react-cookiehub covers the scripts you add through its components. Scripts your app loads another way aren’t held back by the package on their own: load them through CookieHubScript or tag them for consent. Custom tracking packages should check existing consent in onInitialise and start on newly granted consent in onAllow.

How script blocking works
  • Scripts in your components

    CookieHubScript takes an external script (src) or an inline script (innerHTML) and the category it belongs to, and runs it once the visitor allows that category.

  • Google Analytics and Meta Pixel

    CookieHubGA holds Google Analytics until analytics consent; CookieHubPixel holds the Meta (Facebook) Pixel until marketing consent.

  • Other React tracking packages

    Packages that need their own initialization can start in the onInitialise event, or in onAllow once a category such as analytics is allowed.

  • Embedded YouTube videos

    CookieHubYoutube embeds a video from its URL or video ID through a youtube-nocookie URL, with an optional width and height.

Consent events

React when consent changes.

When a visitor changes their choice, your app may need to respond straight away — start a tracking package, stop one or update the interface. Pass event handlers to CookieHub.initialize() to act on consent without a page refresh:

  • onInitialise runs once CookieHub has initialized
  • onAllow runs when the visitor allows a category, such as analytics
  • onRevoke runs when the visitor revokes a category, such as marketing
  • onStatusChange runs when the visitor updates their consent state

Methods such as hasConsented(category), hasAnswered(), openSettings(), allowAll() and denyAll() let your components check consent, open the settings dialog, or allow or deny all categories. For more options, see the CookieHub JavaScript API.

Explore the JavaScript API
FAQ

React questions, answered.

Load them through react-cookiehub. CookieHubScript takes an external or inline script and the consent category it belongs to, and only runs it once the visitor allows that category. For tracking packages you start in code, check existing consent with hasConsented() in onInitialise, and start them in onAllow when the visitor grants consent later.

Yes. react-cookiehub isn’t tied to a particular router: you initialize CookieHub once in your root component, and client-side route changes don’t reload the page, so the visitor’s consent choice stays in effect as they move between routes. Initialize it at the root rather than in individual routes.

Yes. The CookieHubGA component takes your Google Analytics tracking ID and loads Google Analytics under the analytics category, so it doesn’t run until the visitor allows analytics cookies.

Yes. The CookieHubPixel component takes your Meta (Facebook) Pixel ID and loads the pixel under the marketing category, so it doesn’t run until the visitor allows marketing cookies.

Pass event handlers when you initialize CookieHub: onAllow and onRevoke receive the category the visitor allowed or revoked, and onStatusChange runs when they update their choice. You can also check consent at any time with hasConsented(category), or use the CookieHub JavaScript API.

In the CookieHub dashboard. Each domain you add is assigned a unique 8-character domain code, shown in your list of domains. Pass it to CookieHub.initialize() in place of YOUR-DOMAIN-CODE.

Yes. The react-cookiehub package guide covers both the Next.js App Router and the Pages Router. It also notes that Next.js can cause problems with the load order of scripts, so it recommends loading tracking scripts with CookieHub’s script components.

Yes. CookieHub relies on browser APIs such as window, so the package documentation recommends client-side initialization. With the App Router, call CookieHub.initialize() inside useEffect in a client component rendered from your root layout; with the Pages Router, call it inside useEffect in your _app file.

Yes. If your React app already loads its tags through Google Tag Manager, you can install CookieHub through GTM with the CookieHub CMP template instead of react-cookiehub. Use one implementation, not both.

CookieHub provides tools that help React app owners meet consent requirements under privacy laws such as GDPR, CCPA and LGPD: a consent banner, consent-controlled loading for the scripts you add through it, records of consent decisions and a cookie declaration that stays up to date.

Get started

Add consent management to your React app.

Use CookieHub’s React integration to keep visitor consent connected to the scripts and services your application loads.