@use-cooke-consent/core
Install
Using yarn:
yarn add @use-cookie-consent/core
Using npm:
npm install @use-cookie-consent/core
Usage
Below you can see the most basic usage:
import { useCookieConsent } from "@use-cookie-consent/core";
export const YourComponent = () => {
const { consent, acceptAllCookies, declineAllCookies } = useCookieConsent();
// JSX example, but any JS UI libary can be used.
return (
<>
<h2>
Third party cookies {consent.thirdParty ? "accepted" : "declined"}
</h2>
<button onClick={acceptAllCookies}>Accept all</button>
<button onClick={declineAllCookies}>Decline all</button>
</>
);
};
⚠️ Note that this approach does not inform other
useCookieConsent
invocations about a change. This means that if you have twouseCookieConsent
functions, they will have the same state, but if you runacceptAllCookies
on one of them, the other will not be updated without a page refresh, or re-running the function.
If you need to use this function aross different components you should use utilities provided in one of the library-specific packages.
API
useCookieConsent
hook
- Arguments:
options?: CookieConsentOptions
- Configuration object:defaultConsent?: CookieConsent
- The optional initial state of the cookie consent hook.consentCookieAttributes?: CookieAttributes
- Attributes for the underlyingjs-cookie
package, more info here.
- Returns:
consent: CookieConsent
- Cookie consent state:session?: boolean;
- Part of “duration” cookie class.persistent?: boolean;
- Part of “duration” cookie class.necessary?: boolean;
- Part of “purpose” cookie class.preferences?: boolean;
- Part of “purpose” cookie class.statistics?: boolean;
- Part of “purpose” cookie class.marketing?: boolean;
- Part of “purpose” cookie class.firstParty?: boolean;
- Part of “provenance” cookie class.thirdParty?: boolean;
- Part of “provenance” cookie class.
acceptCookies: (cookies: CookieTypes) => void;
- Accept specified cookies.declineAllCookies: () => void;
- Decline all cookies.acceptAllCookies: () => void;
- Accept all cookies.didAcceptAll: () => boolean;
- Check whether all cookies are accepted.didDeclineAll: (opts?: { includingNecessary: boolean; }) => boolean;
- Check whether all cookies were declined, withnecessary
not counting by default.cookies: CookieWrapper;
- Wrapper aroundjs-cookie
with matching API.