c15t
Version:
Headless JavaScript consent management platform for cookie banners, privacy preferences, consent storage, and script gating.
94 lines (67 loc) • 2.3 kB
Markdown
---
title: Setting Consent
description: Reference page for setting consent.
group: reference
---
The primary way to persist consent. Accepts one of three strategies:
```tsx
const { saveConsents } = useConsentManager();
// Accept all - sets every active category to true
await saveConsents('all');
// Reject all - only necessary stays true, everything else false
await saveConsents('necessary');
// Save current selections - persists whatever the user toggled
await saveConsents('custom');
```
**What happens when you call saveConsents:**
1. Consent state is updated in the store
2. UI closes (activeUI → 'none')
3. Consent is saved to localStorage and cookie
4. If consent was revoked and `reloadOnConsentRevoked` is true, the page reloads
5. Otherwise, scripts/iframes/network blocker are updated
6. Consent is synced to the backend API
## setConsent(name, value)
Updates a single consent category AND automatically saves it. Use this for simple one-off consent changes:
```tsx
const { setConsent } = useConsentManager();
// Grant measurement consent immediately
setConsent('measurement', true);
// Revoke marketing consent immediately
setConsent('marketing', false);
```
Updates the selection state without saving. This is what dialog toggles use - the user can flip toggles without committing until they click "Save":
```tsx
const { setSelectedConsent, saveConsents } = useConsentManager();
// User toggles measurement on
setSelectedConsent('measurement', true);
// User toggles marketing off
setSelectedConsent('marketing', false);
// User clicks "Save" - now it persists
await saveConsents('custom');
```
Resets all consent preferences to their default values and clears stored consent:
```tsx
const { resetConsents } = useConsentManager();
resetConsents();
// All consents back to defaults, consent info cleared
```
Common patterns for banner buttons:
```tsx
function ConsentActions() {
const { saveConsents } = useConsentManager();
return (
<div>
<button onClick={() => saveConsents('necessary')}>
Reject All
</button>
<button onClick={() => saveConsents('all')}>
Accept All
</button>
</div>
);
}
```