React hook for managing GDPR cookie consent state.


Although code in this repository is oriented to satisfy GDPR cookie rules, neither author nor contributors to this repository will be responsible for any non-compliance with the law. Please make sure that this repository provides all the functionality to satisfy requirements for your project. If you find something that can be improved please create an issue or send a PR with your fixes!


This React hook is made to make managing cookie consent state easier in a the React hook world. It is following this GDPR cookie guide which describes what you need for GDPR compliance. This hook mainly focuses handling the consent state of the different types of cookies as described in "Types of Cookies" in this page. Summarizing the mentioned document, there are three different ways to classify cookies:

  • Cookie Duration
    • Session cookies
    • Persistent cookies
  • Cookie Provenance
    • First-party cookies
    • Third-party cookies
  • Cookie Purpose
    • Strictly necessary cookies
    • Preferences cookies
    • Statistics cookies
    • Marketing cookies

The hook in this repository will provide a way to manage these types of cookies.


This package can be easily installed using npm:

npm i use-cookie-consent

Or yarn:

yarn add use-cookie-consent


import {useCookieConsent} from 'use-cookie-consent';

export const YourComponent = () => {
  const {consent, acceptAllCookies, declineAllCookies, acceptCookies} =

  return (
        {`Third-party cookies ${consent.thirdParty ? 'approved' : 'rejected'}`}
        {`First-party cookies ${consent.firstParty ? 'approved' : 'rejected'}`}

      <button onClick={acceptAllCookies}>Accept all</button>
      <button onClick={() => acceptCookies({thirdParty: true})}>
        Accept third-party
      <button onClick={() => acceptCookies({firstParty: true})}>
        Accept first-party
      <button onClick={declineAllCookies}>Reject all</button>



useCookieConsent is the main hook in this library. You call it whenever you need to accept, decline, set or get cookies - so anything to do with cookies.

  defaultConsent?: CookieConsent,
  consentCookieAttributes?: CookieAttributes;

This hook function returns following object:

  consent: {
    session?: boolean;
    persistent?: boolean;
    necessary?: boolean;
    preferences?: boolean;
    statistics?: boolean;
    marketing?: boolean;
    firstParty?: boolean;
    thirdParty?: boolean;
  acceptCookies: (cookies: CookieTypes) => void;
  declineAllCookies: () => void;
  acceptAllCookies: () => void;
  didAcceptAll: () => boolean;
  didDeclineAll: (opts?: CookieDeclineOptions) => boolean;
  cookies: CookieWrapper;


  • [ ] Add support for Storage API
  • [ ] Add support for custom cookie categories
  • [ ] Create documentation website
  • [ ] Change CookiesWrapper API to something that doesn't require a specific dependency (maybe just Storage API step?)


Following package was used as a starter for this project: