Skip to main content

ConsentProvider

The ConsentProvider is the main component of React Hook Consent. It provides the consent state to all child components and handles the consent changes.

Usage

/*
* index.tsx
*/

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

const options: ConsentOptions = {
// configure here
}

// ...
<ConsentProvider options={options}>
// App comes here
</ConsentProvider>;
// ...

API

ConsentOptions

ConsentOptions is the main configuration object for the ConsentProvider. It has the following options:

NameTypeRequiredDescriptionDefault
servicesConsentOptionsService[]yesThe configuration of the services to be covered
customHashstringA custom hash to detect if options have been updated
themeThemeConfiguration of light or dark themedark

ConsentOptionsService

ConsentOptionsService is the configuration object for a service. It has the following options:

NameTypeRequiredDescription
idstringyesA unique id for the service, e.g. 'myid'
namestringyesThe name of the service, e.g. 'My Service'
descriptionstringThe 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
localStoragestring[]Configuration of local storage to delete them upon decline
sessionStoragestring[]Configuration of session storage to delete them upon decline
mandatorybooleanIf true, the service is mandatory and cannot be declined

Cookie is the configuration object for a cookie. It has the following options:

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

ScriptExternal

ScriptExternal is the configuration object for an external script. It 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

ScriptCode is the configuration object for embedded JS code. It 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");