Skip to main content

API

Configuration

The configuration is coming together based on some attributes, you are able to customize.

AttributeTypePurpose
categoriesreadonly Category[]List of categories, you want to use.
integrations?ConsentIntegration[]List of integrations, you want to use.
cookieName?stringName of the cookie, you want to use for saving.
cookieExpiresIn?numberTime in days, when the cookie should expire
shouldLoadConsentOnLoad?booleanShould the cookie be loaded on initial load?
defaultCategoryConsent?ConsentCategoryConsentDefault consent for categories
defaultIntegrationsConsent?ConsentIntegrationConsentDefault consent for integrations
Note: Attributes marked with a `?` are optional.
Note 2: Types, that use `categories`, are most likely generic types with a strict appearance of `Category`

ConsentManagerProvider Props

The ConsentManagerProvider component – at this moment – only accepts one prop (technically two).

Prop nameTypePurpose
onConsentSaved(c: ConsentCookieValue) => stringHook that is called, when cookie is saved.
childrenReactNodeEither a react node (normal react component)
children(v: ConsentManagerContextValue) => ReactNodeOr use a function in the provider.
info

Pay attention, when shouldLoadConsentOnLoad is true, the onConsentSaved method is called on each render (with according attribute).

useConsentManager attributes

Managing category preferences

AttributeType
categoriesreadonly Category[]
categoryPreferencesConsentCategoryConsent<Category>
setCategoryPreferences(p: Partial<ConsentCategoryConsent<Category>>) => void

Managing integration preferences

AttributeType
integrationsConsentIntegration<Category>[]
integrationPreferencesConsentIntegrationConsent<Category>
setIntegrationPreferences(p: Partial<ConsentIntegrationConsent<Category>>) => void
AttributeTypePurpose
consentDateDate
hasConsentGivenbooleantrue is user has already given consent once
changedSinceConsentDatebooleantrue, if consentDate < last added integration

The following two are just plain-ol React state, use them as you would just use useState.

AttributeType
shouldBeDisplayedboolean
setShouldBeDisplayedDispatch<SetStateAction<boolean>>
AttributeTypePurpose
saveConsentFunctionSaves the consent, and accepts whole new stack of consent.
executeConsentFunctionExecutes the consent and calls onConsentSaved

"Accept all" functionality

Because we are using useState and some internal calculations, it is not possible to do the following.

// Don't do this.
function onClick() {
setCategoryPreferences({ category1: true });
setCategoryPreferences({ category2: true });
setCategoryPreferences({ category3: true });
saveConsent();
}

// Do this.
function onClick() {
saveConsent({
categories: { category1: true, category2: true, category3: true },
});
}