Socket
Socket
Sign inDemoInstall

@use-cookie-consent/react

Package Overview
Dependencies
8
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @use-cookie-consent/react

useCookieConsent hook for React apps


Version published
Weekly downloads
983
increased by5.7%
Maintainers
1
Install size
135 kB
Created
Weekly downloads
 

Readme

Source

useCookieConsent hook for React.js

Build NPM Version NPM Downloads Codecov gzipped size License

"Buy Me A Coffee"

Headless state management for GDPR cookie consent

  • Headless - bring your own styles, we will provide the logic.
  • Hook-based - extremely intuitive for React developers.
  • React Context support - wrap your app in a provider and synchronse UI updates across the app.
  • Tiny - less than 2kB gzipped.

Description

This package provides a wrapper around @use-cookie-consent/core package to provide best experience for React applications. Namely, it provides a React context, which provides all the functionality of core package but with reactive updates.

Installation

npm install @use-cookie-consent/react

or

yarn add @use-cookie-consent/react

Usage

Basic usage

The first step to this setup is to wrap your app in the CookieConsentProvider:

import { CookieConsentProvider } from '@use-cookie-consent/react'

export default function App() {
  return {
    <CookieConsentProvider>
      ...
    </CookieConsentProvider>
  }
}

Then you can read and update the cookie consent data from any component which is inside of the <CookieConsentProvider>. Both components you see below are not connected to each other in any way except the useCookieConsentContext function. You would put both of them separately somewhere in your app WITHOUT connecting them by any callbacks or passing state through props.

// This component is an example of a component that you use to
// accept/decline cookie consent, without reading any data.
const CookieBanner = () => {
  const { acceptAllCookies, declineAllCookies, acceptCookies } =
    useCookieConsentContext();

  return (
    <div>
      <button onClick={acceptAllCookies}>Accept all</button>
      <button onClick={() => acceptCookies({ thirdParty: true })}>
        Accept third-party
      </button>
      <button onClick={() => acceptCookies({ firstParty: true })}>
        Accept first-party
      </button>
      <button onClick={declineAllCookies}>Reject all</button>
    </div>
  );
};
// This component is an example of a component which would show
// the state of the cookie consent, without updating anything.
const CookiesPreview = () => {
  const { consent } = useCookieConsentContext();

  return (
    <div>
      <div>
        {`Third-party cookies ${consent.thirdParty ? 'approved' : 'rejected'}`}
      </div>
      <div>
        {`First-party cookies ${consent.firstParty ? 'approved' : 'rejected'}`}
      </div>
    </div>
  );
};

Thanks to @pixelass for prividing the examples you see above.

Contributors

Discussions and Questions

For non-issues please consider joining our Discord here!

License

MIT

Keywords

FAQs

Last updated on 28 Aug 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc