TwitterGitHub
Dark LogoStratik / UIbeta
useCookies

The useCookies hook a way to manage cookie preferences and operations in a React application. It allows you to toggle, reset, accept, or reject cookie categories, as well as set, edit, remove, and retrieve cookies with ease. This hook leverages localStorage to persist cookie preferences across sessions, ensuring a consistent user experience.

Options Table
NameTypeDefaultDescription
pathstring"/"Path within the domain that the cookie is accessible to.
domainstring-Domain that the cookie is accessible to.
secureboolean-Indicates if the cookie should be transmitted over HTTPS only.
sameSite"strict" | "lax" | "none"-Controls the cookie's same-site policy.
Return Table
NameTypeDescription
cookiePreferencesCookiePreferencesThe current user's cookie preferences.
toggleCookieCategory(category: CookieCategory) => voidToggles the acceptance of cookies for a specific category.
resetCookiePreferences() => voidResets the cookie preferences to the default values.
acceptAllCookiePreferences() => voidAccepts all categories of cookies.
rejectAllCookiePreferences() => voidRejects all categories of cookies.
setCookie(name: string, value: string, time: number, category: CookieCategory, options?: CookieOptions) => voidSets a cookie with the specified name, value, expiration time, and options.
editCookie(category: CookieCategory, name: string, value: string, days: number, options?: CookieOptions) => voidEdits an existing cookie with the specified details.
removeCookie(name: string, category: CookieCategory) => voidRemoves a cookie by name for a specific category.
getCookie(name: string) => string | nullRetrieves the value of a cookie by name.
getAllCookies() => ObjectReturns all cookies as an object with key-value pairs.
Cookie Category Enum

The CookieCategory type can be one of the following strings:

  • "essential"
  • "functional"
  • "analytics"
  • "advertising"
Parameters Table for Functions

toggleCookieCategory

NameTypeDescription
categoryCookieCategoryThe category of cookies to toggle.

setCookie

NameTypeDefaultDescription
namestring-The name of the cookie to set.
valuestring-The value to assign to the cookie.
timenumber-The time in seconds for which the cookie is valid.
categoryCookieCategory-The category of the cookie to set.
optionsCookieOptions{ path: "/" }Optional settings for cookie attributes like path, domain, etc.

editCookie

NameTypeDefaultDescription
categoryCookieCategory-The category of the cookie to edit.
namestring-The name of the cookie to edit.
valuestring-The new value to assign to the cookie.
daysnumber-The number of days for which the cookie should be valid.
optionsCookieOptions{}Optional settings for cookie attributes like path, domain, etc.

removeCookie

NameTypeDescription
namestringThe name of the cookie to remove.
categoryCookieCategoryThe category of the cookie to remove.

getCookie

NameTypeDescription
namestringThe name of the cookie to retrieve.

useCookie

Click any of the buttons to get started.