A powerful, customizable React component for cookie consent management with built-in tracking prevention. This component provides a modern, user-friendly way to obtain and manage cookie consent from your website visitors.
- ๐ Multiple display types (banner, popup, modal)
- ๐ก๏ธ Automatic tracking prevention (Google Analytics, etc.)
- ๐ฏ Granular cookie category controls (Analytics, Social, Advertising)
- ๐จ Light and dark theme support
- ๐ฑ Responsive design
- ๐ง Highly customizable UI
- ๐พ Persistent consent storage
- ๐ Privacy-first approach
- ๐ช๐บ Note: Full GDPR compliance requires you to store user consent in your own database.
See React Cookie Manager in action and explore all its features in our interactive demo.
Unlike other cookie consent managers and React components, this component automatically disables tracking for Google Analytics, Facebook Pixel, and other tracking services. This is done by blocking the tracking scripts from loading. Therefore, you don't need to manually disable tracking, saving you hours of work.
npm install react-cookie-manager
# or
yarn add react-cookie-managerThe component requires its CSS file to be imported in your application. Add the following import to your app's entry point (e.g., App.tsx or index.tsx):
import "react-cookie-manager/style.css";import { CookieManager } from "react-cookie-manager";
import "react-cookie-manager/style.css";
function App() {
return (
<CookieManager
translations={{
title: "Cookie Preferences",
message: "We use cookies to improve your experience."
}}
onManage={(preferences) =>
console.log("Cookie preferences:", preferences)
}
>
<YourApp />
</CookieManager>
);
}import { CookieManager } from "react-cookie-manager";
import "react-cookie-manager/style.css";
function App() {
return (
<CookieManager
translations={{
title: "Would You Like A Cookie? ๐ช",
message: "We value your privacy. Choose which cookies you want to allow. Essential cookies are always enabled as they are necessary for the website to function properly.",
buttonText: "Accept All",
declineButtonText: "Decline All",
manageButtonText: "Manage Cookies",
privacyPolicyText: "Privacy Policy"
}}
showManageButton={true}
privacyPolicyUrl="https://example.com/privacy"
theme="light"
displayType="popup"
onManage={(preferences) => {
if (preferences) {
console.log("Cookie preferences updated:", preferences);
}
}}
>
<AppContent />
</CookieManager>
);
}import { CookieManager, useCookieConsent } from "react-cookie-manager";
function CookieSettings() {
const { showConsentBanner, detailedConsent } = useCookieConsent();
return (
<div>
<button onClick={showConsentBanner}>Manage Cookie Settings</button>
{detailedConsent && (
<div>
Analytics:{" "}
{detailedConsent.Analytics.consented ? "Enabled" : "Disabled"}
Social: {detailedConsent.Social.consented ? "Enabled" : "Disabled"}
Advertising:{" "}
{detailedConsent.Advertising.consented ? "Enabled" : "Disabled"}
</div>
)}
</div>
);
}| Prop | Type | Default | Description |
|---|---|---|---|
children |
React.ReactNode | - | Your app components |
translations |
TranslationObject | TranslationFunction | - | Translation object or i18n TFunction |
translationI18NextPrefix |
string | - | i18next key prefix, e.g. "cookies." |
showManageButton |
boolean | false | Whether to show the manage cookies button |
privacyPolicyUrl |
string | - | URL for the privacy policy |
localStorageKey |
string | 'cookie-consent' | Name of the key to store consent |
cookieExpiration |
number | 365 | Days until cookie expires |
displayType |
'banner' | 'popup' | 'modal' | 'banner' | How the consent UI is displayed |
position |
'top' | 'bottom' | 'bottom' | Position of the banner |
theme |
'light' | 'dark' | 'light' | Color theme |
disableAutomaticBlocking |
boolean | false | Disable automatic tracking prevention |
blockedDomains |
string[] | [] | Additional domains to block |
onManage |
(preferences?: CookieCategories) => void | - | Callback when preferences are updated |
The component manages three categories of cookies:
interface CookieCategories {
Analytics: boolean;
Social: boolean;
Advertising: boolean;
}The useCookieConsent hook provides the following:
interface CookieConsentHook {
hasConsent: boolean | null;
isDeclined: boolean;
detailedConsent: DetailedCookieConsent | null;
showConsentBanner: () => void;
acceptCookies: () => void;
declineCookies: () => void;
updateDetailedConsent: (preferences: CookieCategories) => void;
}import { default as i18next } from "i18next";
function App() {
return (
<CookieManager
translations={i18next.t}
translationI18NextPrefix="cookies."
...
/>
)
}// en.json
{
"cookies": {
"title": "Would You Like A Cookie? ๐ช",
"message": "We value your privacy. Choose which cookies you want to allow. Essential cookies are always enabled as they are necessary for the website to function properly.",
"buttonText": "Accept All",
"declineButtonText": "Decline All",
"manageButtonText": "Manage Cookies",
"privacyPolicyText": "Privacy Policy"
}
//...
}Contributions are welcome! Please feel free to submit a Pull Request.
MIT ยฉ Hypership





