Skip to content

useConsent for Vue

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 computed ref 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/components/ConsentStatus.vue
<script setup lang="ts">
import { useConsent } from "@cookieyes/vue";

const consent = useConsent();
</script>

<template>
  <p v-if="!consent.hasActed">No choice made yet.</p>
  <ul v-else>
    <li v-for="(granted, id) in consent.committedCategories" :key="id">
      {{ id }}: {{ granted ? "allowed" : "blocked" }}
    </li>
  </ul>
</template>

What it returns

The fields below are on the ref's .value. In a template Vue unwraps the ref, so you write consent.hasActed.

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.

During server rendering and the hydrating render it returns a fresh-visitor snapshot: hasActed: false, only required categories granted. If a CookieYesProvider above it was given initialConsent, the snapshot carries that visitor's saved choice instead. It switches to the live state once the component has mounted.

It throws if app.use(CookieYes, config) has not run, and must be called inside setup().

Common mistakes

A tracker loads the moment someone flips a switch. You read categories. Read committedCategories, or use useConsentCategory().

It throws "could not find CookieYes". app.use(CookieYes, config) did not run before the app mounted, or you called it outside setup(). See Installation → Common mistakes.

hasActed is true but every category is false. The visitor rejected everything. Correct.

Next steps

On this page