Skip to content

useConsent for Nuxt

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

app/components/ConsentStatus.vue
<script setup lang="ts">
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>

Nuxt imports useConsent for you, and <ConsentStatus /> from app/components.

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. Inside <CookieYesRoot>, the snapshot carries the saved choice the module read from the request instead. It switches to the live state once the component has mounted.

Call it inside setup() or <script setup>.

Common mistakes

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

useConsent is not defined. @cookieyes/nuxt is missing from modules in nuxt.config.ts, so nothing is imported for you. See Installation.

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

Next steps

On this page