Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useCookies.md
1---2category: '@Integrations'3---45# useCookies67Wrapper for [`universal-cookie`](https://www.npmjs.com/package/universal-cookie).89::: tip10When using with Nuxt 3, this functions will **NOT** be auto imported in favor of Nuxt's built-in [`useCookie()`](https://v3.nuxtjs.org/api/composables/use-cookie). Use explicit import if you want to use the function from VueUse.11:::1213## Install1415```bash16npm i universal-cookie@^717```1819## Usage2021### Common usage2223```vue24<script setup lang="ts">25import { useCookies } from '@vueuse/integrations/useCookies'2627const cookies = useCookies(['locale'])28</script>2930<template>31<div>32<strong>locale</strong>: {{ cookies.get('locale') }}33<hr>34<pre>{{ cookies.getAll() }}</pre>35<button @click="cookies.set('locale', 'ru-RU')">36Russian37</button>38<button @click="cookies.set('locale', 'en-US')">39English40</button>41</div>42</template>43```4445## Options4647Access and modify cookies using vue composition-api.4849> By default, you should use it inside `setup()`, but this function also works anywhere else.5051```ts52import { useCookies } from '@vueuse/integrations/useCookies'53// ---cut---54const {55get,56getAll,57set,58remove,59addChangeListener,60removeChangeListener61} = useCookies(['cookie-name'], {62doNotParse: false,63autoUpdateDependencies: false64})65```6667### `dependencies` (optional)6869Let you optionally specify a list of cookie names your component depend on or that should trigger a re-render. If unspecified, it will render on every cookie change.7071### `options` (optional)7273- `doNotParse` (boolean = false): do not convert the cookie into an object no matter what. **Passed as default value to `get`/`getAll` methods.**74- `autoUpdateDependencies` (boolean = false): automatically add cookie names ever provided to `get` method. If **true** then you don't need to care about provided `dependencies`.7576### `cookies` (optional)7778Let you provide a `universal-cookie` instance (creates a new instance by default)7980> Info about methods available in the [universal-cookie api docs](https://www.npmjs.com/package/universal-cookie#api---cookies-class)8182## `createCookies([req])`8384Create a `universal-cookie` instance using request (default is window.document.cookie) and returns `useCookies` function with provided universal-cookie instance8586- req (object): Node's [http.IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage) request object8788## Type Declarations8990```ts91/**92* Creates a new {@link useCookies} function93* @param req - incoming http request (for SSR)94* @see https://github.com/reactivestack/cookies/tree/master/packages/universal-cookie universal-cookie95* @description Creates universal-cookie instance using request (default is window.document.cookie) and returns {@link useCookies} function with provided universal-cookie instance96*/97export declare function createCookies(req?: IncomingMessage): (98dependencies?: string[] | null,99{100doNotParse,101autoUpdateDependencies,102}?: {103doNotParse?: boolean | undefined104autoUpdateDependencies?: boolean | undefined105},106) => {107/**108* Reactive get cookie by name. If **autoUpdateDependencies = true** then it will update watching dependencies109*/110get: <T = any>(name: string, options?: CookieGetOptions | undefined) => T111/**112* Reactive get all cookies113*/114getAll: <T = any>(options?: CookieGetOptions | undefined) => T115set: (116name: string,117value: any,118options?: CookieSetOptions | undefined,119) => void120remove: (name: string, options?: CookieSetOptions | undefined) => void121addChangeListener: (callback: CookieChangeListener) => void122removeChangeListener: (callback: CookieChangeListener) => void123}124/**125* Reactive methods to work with cookies (use {@link createCookies} method instead if you are using SSR)126* @param dependencies - array of watching cookie's names. Pass empty array if don't want to watch cookies changes.127* @param options128* @param options.doNotParse - don't try parse value as JSON129* @param options.autoUpdateDependencies - automatically update watching dependencies130* @param cookies - universal-cookie instance131*132* @__NO_SIDE_EFFECTS__133*/134export declare function useCookies(135dependencies?: string[] | null,136{137doNotParse,138autoUpdateDependencies,139}?: {140doNotParse?: boolean | undefined141autoUpdateDependencies?: boolean | undefined142},143cookies?: Cookie,144): {145/**146* Reactive get cookie by name. If **autoUpdateDependencies = true** then it will update watching dependencies147*/148get: <T = any>(name: string, options?: CookieGetOptions | undefined) => T149/**150* Reactive get all cookies151*/152getAll: <T = any>(options?: CookieGetOptions | undefined) => T153set: (154name: string,155value: any,156options?: CookieSetOptions | undefined,157) => void158remove: (name: string, options?: CookieSetOptions | undefined) => void159addChangeListener: (callback: CookieChangeListener) => void160removeChangeListener: (callback: CookieChangeListener) => void161}162```163