Skip to main content

useCookie

The useCookie hook provides a reactive interface for managing browser cookies. More Info...

import { useCookie } from '@archibald/server';

const [value, set, clear] = useCookie(NAME, OPTIONS);

Parameters

NameTypeRequiredDescription
namestring✔️The name of the cookie.
optionsCookieOptionsConfiguration for the hook.

CookieOptions

PropertyTypeDefaultDescription
updateOnRefocusbooleantrueIf true, the hook re-reads the cookie value when the window gains focus.

Return Value

  • Type: readonly [string, (value: string, options?: CookieSetOptions) => void, (options?: CookieSetOptions) => void]

The hook returns a tuple containing:

  1. The current cookie value.
  2. A setter function to update the cookie.
  3. A clear function to remove the cookie.