Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-hook-consent

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-hook-consent

React consent management solution and banner for cookies and (external) scripts.

  • 2.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
538
increased by40.47%
Maintainers
1
Weekly downloads
 
Created
Source

React consent management solution and banner for cookies and (external) scripts.

react-hook-consent Screenshot

Features

  • Provides the consent context to components
  • Loads (external) scripts based on consent state
  • Deletes cookies when consent declined
  • Hook to retrieve and change the current consent
  • Optional Banner with detailed settings to approve / decline consent
  • Persists the selection to local storage
  • Ready for Next.js
  • Dark and light mode
  • Styling via css

Examples

Check out the CodeSandbox for a working example.

More examples for React and Next.js are included in the examples folder of the repository.

Installation

yarn add react-hook-consent

# or

npm install react-hook-consent

Basic usage

Provider

Wrap the application in the ConsentProvider. Provide settings via the options prop.

/*
 * index.tsx
 */

import { ConsentProvider } from 'react-hook-consent';

// ...
<ConsentProvider
    options={{
        services: [
            {
                id: 'myid',
                name: 'MyName',
                scripts: [
                    { id: 'external-script', src: 'https://myscript.com/script.js' },
                    { id: 'inline-code', code: `alert("I am a JavaScript code");` },
                ],
                cookies: [{ pattern: 'cookie-name' }],
            },
        ],
        theme: 'light',
    }}
>
    <App />
</ConsentProvider>;
// ...
Options Prop
NameTypeRequiredDescriptionDefault
servicesConsentOptionsService[]yesThe configuration of the services to be covered
themeThemeConfiguration of light or dark themedark

The services array can be configured as follows:

NameTypeRequiredDescription
idstringyesA unique id for the service, e.g. 'myid'
namestringyesThe name of the service, e.g. 'My Service'
namestringThe description of the service, e.g. 'My ID is a tracking service.'
scriptsArray<ScriptExternal | ScriptCode>External script or code to load upon consent
cookiesCookie[]Configuration of cookies to delete them upon decline

ScriptExternal has the following options:

NameTypeRequiredDescription
idstringyesA unique id for the script, e.g. 'myscript'
srcstringyesThe link to the script, e.g. 'https://myscript.com/script.js'

ScriptCode has the following options:

NameTypeRequiredDescription
idstringyesA unique id for the script, e.g. 'myscript'
codestringyesThe js Code of the script, e.g. alert("I am a JavaScript code");

Cookie has the following options:

NameTypeRequiredDescription
patternstring | RegExpyesThe name or a regex pattern of the cookie

Banner

Optionally use the banner component to render a banner to show a consent dialogue.

/*
 * index.tsx
 */

import { ConsentBanner } from 'react-hook-consent';

// styling
import 'react-hook-consent/dist/styles/style.css';

// ...
<App />
<ConsentBanner
    details={{ hidden: false, label: 'More' }}
    decline={{ label: 'No' }}
    approve={{ label: 'Yes' }}
>
     <>
        Can we use cookies and external services according to our <a href="test">privacy policy</a> to
        improve the browsing experience?
    </>
</ConsentBanner>
// ...
Props
NameTypeRequiredDescriptionDefault
childrenReactNodeThe consent messageWe want to use cookies and external services to analyze and improve this website for you. You will find more details in our privacy policy.
details.hiddenbooleanHide the detailed settings buttonfalse
details.labelstring | ReactNodeLabel of detailed settings buttonSettings
approve.labelstring | ReactNodeLabel of approve buttonApprove
decline.labelstring | ReactNodeLabel of decline buttonDecline

Hooks

Use the provided useConsent hook to access or update the current consent and show or hide the banner.

/*
 * App.tsx
 */

import { useConsent } from 'react-hook-consent';

// ...
const { consent, setConsent, isBannerVisible, toggleBanner } = useConsent();
// ...
Return
Object NameTypeDescription
consentConsent[]Services which have been consent to
isBannerVisiblebooleanIndicates if consent banner is visible
toggleBanner() => voidShows or hides the consent banner
setConsent(consent: Consent[]) => voidUpdate consent by providing consent ids to be saved

Keywords

FAQs

Package last updated on 11 Jan 2023

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc