Skip to content

React component for TinyConsent cookie consent banners. One component for cookie consent in React, Next.js, Gatsby.

License

Notifications You must be signed in to change notification settings

TinyConsent/tinyconsent-react

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tinyconsent-react

npm version License: MIT TypeScript

The easiest cookie banner React component. One component, full GDPR/CCPA compliance.

A lightweight React component for adding TinyConsent cookie consent banners to your React, Next.js, Gatsby, or any React-based application.

Why TinyConsent React?

  • ⚛️ React-native - Proper React component, not a script hack
  • 🚀 One component - Just <TinyConsentScript id="..." />
  • 📦 Tiny - Under 1KB, loads the actual banner async
  • Cookie consent handling - Blocks scripts until user consents
  • 🔧 Google Consent Mode v2 - Automatic GA4 integration
  • 🎨 Customizable - Configure via dashboard, no code changes
  • 📱 SSR Ready - Works with Next.js, Remix, Gatsby

Get your script ID: tinyconsent.com/cookie-banner-generator

Installation

npm install tinyconsent-react
yarn add tinyconsent-react
pnpm add tinyconsent-react

Quick Start

import { TinyConsentScript } from 'tinyconsent-react';

function App() {
  return (
    <>
      <TinyConsentScript id="your-script-id" />
      <div>My App Content</div>
    </>
  );
}

That's it! Your cookie consent banner will appear automatically.

What It Renders

The <TinyConsentScript /> component renders:

<script src="https://scripts.tinyconsent.com/api/scripts/your-script-id" async></script>

No internal logic, no bloat - just the script tag that loads your cookie banner.

API Reference

<TinyConsentScript />

React component that renders the TinyConsent script tag.

<TinyConsentScript
  id="your-script-id"        // Required: Your TinyConsent script ID
  async={true}               // Optional: Load async (default: true)
  defer={false}              // Optional: Defer execution (default: false)
  nonce="abc123"             // Optional: CSP nonce
  onLoad={() => {}}          // Optional: Called when loaded
  onError={(err) => {}}      // Optional: Called on error
/>

useTinyConsent(scriptId)

React hook for programmatic loading.

import { useTinyConsent } from 'tinyconsent-react';

function App() {
  const { isLoading, isLoaded, error } = useTinyConsent('your-script-id');
  
  if (isLoading) return <div>Loading...</div>;
  if (error) return <div>Error loading banner</div>;
  
  return <div>My App</div>;
}

getTinyConsentScriptUrl(scriptId)

Get the full script URL.

import { getTinyConsentScriptUrl } from 'tinyconsent-react';

const url = getTinyConsentScriptUrl('your-script-id');
// 'https://scripts.tinyconsent.com/api/scripts/your-script-id'

Framework Examples

Next.js (App Router)

// app/layout.tsx
import { TinyConsentScript } from 'tinyconsent-react';

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <head>
        <TinyConsentScript id="your-script-id" />
      </head>
      <body>{children}</body>
    </html>
  );
}

Next.js (Pages Router)

// pages/_app.tsx
import { TinyConsentScript } from 'tinyconsent-react';

export default function App({ Component, pageProps }) {
  return (
    <>
      <TinyConsentScript id="your-script-id" />
      <Component {...pageProps} />
    </>
  );
}

Gatsby

// gatsby-browser.js
import React from 'react';
import { TinyConsentScript } from 'tinyconsent-react';

export const wrapRootElement = ({ element }) => (
  <>
    <TinyConsentScript id="your-script-id" />
    {element}
  </>
);

Create React App / Vite

// src/App.tsx
import { TinyConsentScript } from 'tinyconsent-react';

function App() {
  return (
    <>
      <TinyConsentScript id="your-script-id" />
      <div className="App">
        {/* Your app content */}
      </div>
    </>
  );
}

Remix

// app/root.tsx
import { TinyConsentScript } from 'tinyconsent-react';

export default function App() {
  return (
    <html lang="en">
      <head>
        <Meta />
        <Links />
        <TinyConsentScript id="your-script-id" />
      </head>
      <body>
        <Outlet />
        <Scripts />
      </body>
    </html>
  );
}

What is a Cookie Banner?

A cookie banner (cookie consent banner) collects user consent for cookies and tracking. It:

  1. Informs users about cookies and tracking
  2. Gets consent before setting non-essential cookies
  3. Provides control to accept, reject, or customize
  4. Respects choices by blocking scripts until consent

Learn more: What is a Cookie Banner?

Cookie Banners vs. Full Compliance

What a Cookie Banner Does

A cookie banner handles consent collection:

  • Displays notice about cookies
  • Collects user preferences
  • Blocks non-essential scripts until consent
  • Stores consent choices

What Full Compliance Requires

Cookie consent is one part of privacy compliance. Full compliance also needs:

  • Privacy Policy - Document explaining data practices
  • Data Processing Records - Internal documentation
  • User Rights Handling - Data access/deletion processes

TinyConsent handles cookie consent. For privacy policies: tinyconsent.com/privacy-policy-generator

GDPR Cookie Consent Requirements

The GDPR requires for cookies:

  • Get consent BEFORE setting non-essential cookies
  • Easy reject option required
  • Granular consent options (analytics, marketing)
  • No pre-ticked boxes

Learn more: GDPR Cookie Requirements

CCPA Requirements

The CCPA requires:

  • Disclose data collection practices
  • "Do Not Sell" option for users
  • Non-discrimination for opt-outs

Learn more: CCPA Cookie Requirements

FAQ

How do I get a script ID?

  1. Visit tinyconsent.com/cookie-banner-generator
  2. Enter your email
  3. Copy your script ID

Does this work with Next.js 13+ App Router?

Yes! Add the component to your root layout's <head>.

Does it block Google Analytics?

Yes. TinyConsent blocks GA4, Facebook Pixel, and other trackers until consent is given.

Does it support Google Consent Mode v2?

Yes. Google Consent Mode is automatically integrated.

Can I customize the banner?

Yes! All customization is done in your TinyConsent Dashboard - no code changes needed.

Do I need a privacy policy?

Yes. Generate one free: tinyconsent.com/privacy-policy-generator

Is it SSR compatible?

Yes. Works with Next.js, Remix, Gatsby, and other SSR frameworks.

Troubleshooting

Banner not appearing

  1. Verify your script ID is correct
  2. Check browser console for errors
  3. Test in incognito mode (clears previous consent)
  4. Ensure no ad blockers are interfering

Hydration warnings in Next.js

The component is designed to avoid hydration issues. If you see warnings, ensure you're using the latest version.

TypeScript errors

Make sure you have @types/react installed:

npm install -D @types/react

TypeScript

Full TypeScript support:

import { 
  TinyConsentScript,
  TinyConsentScriptProps,
  useTinyConsent,
  UseTinyConsentResult,
  getTinyConsentScriptUrl
} from 'tinyconsent-react';

Related Packages

Resources

License

MIT © TinyConsent


Keywords: cookie banner react, react cookie consent, cookie banner npm, GDPR react, CCPA react, cookie consent component, next.js cookie banner, gatsby cookie banner, one line cookie banner, lightweight cookie consent, cookie banner generator

About

React component for TinyConsent cookie consent banners. One component for cookie consent in React, Next.js, Gatsby.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published