# How do I add a cookie consent banner to a React or Next.js app? - CookieBastion

Source: https://cookiebastion.com/blog/add-cookie-consent-banner-to-react-nextjs-app.html

[Home](../index.md) / [Blog](index.md) / How do I add a cookie consent banner to a React or Next.js app?


Add the consent script as early as possible in the document head, gate every non-essential script behind the visitor's consent state, and expose the choice to your components. In Next.js, load the banner before interactive hydration and use the consent state to conditionally render analytics and pixel components.

## Load order is everything

The banner must run before any tracker. In practice that means the consent script loads in the head with high priority, sets a default denied state, and only after that may analytics or pixel code initialize. A banner that hydrates after your tags have already fired has failed its only job.

## Gating scripts in React

Wrap third-party integrations in components that read the consent state from context and render nothing until consent exists for their category. For server-rendered Next.js apps, check the stored consent before emitting script tags so denied visitors never receive the tracker code at all.

## Passing consent to Google

If you use Google tags, wire the consent state into Consent Mode v2 signals so analytics\_storage and ad\_storage reflect the visitor's choice. This keeps measurement behavior lawful in the EU and UK and enables conversion modeling for denied visitors.

## Related reading

[What is a consent management API?](what-is-a-consent-management-api.md)[What is a consent string (TC string)?](https://cookiebastion.com/glossary/consent-string.md)[Cookie consent for React applications](https://cookiebastion.com/use-cases/react-applications.md)[Looking for a Cookiebot alternative?](https://cookiebastion.com/alternatives/cookiebot-alternative.md)

## Get a free consent audit of your website

[Free consent audit](mailto:hello@cookiebastion.com?subject=Free%20consent%20audit%20-%20cookiebastion.com)
