useConsent for SvelteKit
Read the visitor's current consent state in a component, re-rendering whenever it changes, for settings pages and status displays.
What it does
Returns the visitor's current consent as a reactive object and updates your component whenever it changes. Use it to show state: a settings page, a "your choices" panel, a status line. To change consent, use useConsentActions().
Example
<script lang="ts">
import { useConsent } from "@cookieyes/sveltekit";
const consent = useConsent();
</script>
{#if !consent.hasActed}
<p>No choice made yet.</p>
{:else}
<ul>
{#each Object.entries(consent.committedCategories) as [id, granted] (id)}
<li>{id}: {granted ? "allowed" : "blocked"}</li>
{/each}
</ul>
{/if}What it returns
Read the fields where Svelte tracks them: in the template, as above, or in a $derived. Destructuring, as in const { hasActed } = useConsent(), reads the value once and never updates; write const hasActed = $derived(consent.hasActed) instead.
| Field | Meaning |
|---|---|
hasActed | true once the visitor has accepted, rejected or saved. Rejecting everything counts |
committedCategories | The saved values: category id → granted, as last saved. Read this one |
categories | The live values: the same map, but it changes while the visitor flips switches in the dialog, before Save |
regulation | "GDPR" or "CCPA" |
isPreferencesOpen, isOptOutOpen | Whether a dialog is open right now |
isBannerDismissed | true after the visitor closed the banner without choosing, until the next page load |
consentId, lastRenewed | The record id and the time of the last decision |
Saved values vs live values
Two fields look alike:
- Saved values:
committedCategories. They change only when the visitor accepts, rejects, saves or resets. - Live values:
categories. They change on every switch the visitor flips in the dialog, before Save.
Load anything from the saved values, committedCategories: scripts, embeds, trackers. Or use useConsentCategory(id), which reads the saved value for one category and only changes when that one does. Use the live values, categories, only to drive the checkboxes of your own dialog.
Good to know
It updates on every consent change, including a dialog opening or closing. Call it in the component that shows the state, not at the top of your app.
On a page the server rendered per request, the snapshot already carries the visitor's saved choice: loadConsent passes it to <CookieYesRoot> as state={data.consent}. A first-time visitor, and every visitor on a prerendered page, gets a fresh-visitor snapshot during the server render and the hydrating render: hasActed: false, only required categories granted (under CCPA, every category, since consent there is opt-out). It switches to the live state once the root has mounted.
It throws if the component is not rendered inside <CookieYesRoot>, and must be called while the component initialises, not in an event handler.
Common mistakes
A tracker loads the moment someone flips a switch.
You read categories. Read committedCategories, or use useConsentCategory().
The status never updates.
You destructured the result. Keep the object and read consent.hasActed in the template, or wrap it in $derived.
It throws "could not find CookieYes".
The component is not inside <CookieYesRoot>, or you called the hook outside component initialisation. See Installation → Common mistakes.
hasActed is true but every category is false.
The visitor rejected everything. Correct.
Next steps
- useConsentActions: change consent
- Focused hooks:
useConsentCategory()and other one-question hooks - useOnConsentChange: run code when consent changes