Skip to content

useConsent for Svelte

Markdown
Loading…

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

src/lib/ConsentStatus.svelte
<script lang="ts">
  import { useConsent } from "@cookieyes/svelte";

  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.

FieldMeaning
hasActedtrue once the visitor has accepted, rejected or saved. Rejecting everything counts
committedCategoriesThe saved values: category id → granted, as last saved. Read this one
categoriesThe live values: the same map, but it changes while the visitor flips switches in the dialog, before Save
regulation"GDPR" or "CCPA"
isPreferencesOpen, isOptOutOpenWhether a dialog is open right now
isBannerDismissedtrue after the visitor closed the banner without choosing, until the next page load
consentId, lastRenewedThe 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.

When the page was rendered on the server, the server render and the hydrating render return a fresh-visitor snapshot: hasActed: false, only required categories granted (under CCPA, every category, since consent there is opt-out). If the CookieYesProvider above it was given initialConsent, the snapshot carries that visitor's saved choice instead. It switches to the live state once the provider has mounted. A plain client-side app reads the live state at once.

It throws if no <CookieYesProvider> is above the component, 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 <CookieYesProvider>, 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

On this page