VueUse
The community's standard library of composables, what its functions share, the ones you'll reach for first, and when to write your own instead.
VueUse
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- VueUse is a collection of a few hundred tested composables for browser APIs, sensors, element measurement, state persistence, and timing, published as tree-shakable ES modules so you only ship the functions you import.
- Its functions follow the same conventions: arguments accept a ref, a getter, or a plain value, results come back as refs, and listeners or observers are removed automatically when the calling component's scope is disposed.
- useStorage and useLocalStorage return a ref that reads from and writes to Web Storage, serializes non-string values for you, and by default stays in sync across browser tabs.
- onClickOutside, useEventListener, useMouse, and refDebounced replace the hand-written listener and timer code that most components otherwise copy around.
- The current major release of @vueuse/core declares a peer dependency on Vue 3.5 or later; extra packages such as @vueuse/components and @vueuse/integrations add renderless components, directives, and wrappers around third-party libraries.
- Reading a VueUse function's source is one of the best ways to learn how to write production-quality composables, and writing your own is still the right call when the logic is specific to your app.
Why a library of composables exists
Once you’ve written a few composables, a pattern shows up. A large share of them wrap a browser API: track the mouse, watch the window size, persist a value to localStorage, detect a click outside a dropdown, debounce an input. Each of these is ten to thirty lines, and each has edge cases you only learn about by shipping it: the storage event that keeps tabs in sync, the listener you forgot to remove, the window access that crashes server rendering.
Every Vue team was rewriting the same small set of composables. VueUse, started by Anthony Fu (a Vue and Vite core team member), collects them into one library with tests, types, and docs. Think of it as the standard library that the Composition API’s primitives didn’t come with: ref and watch are the bricks, and VueUse is a box of pre-built walls.
Installing and using it
npm i @vueuse/core
Then import what you need:
<script setup>
import { useMouse, useLocalStorage } from '@vueuse/core'
const { x, y } = useMouse()
const theme = useLocalStorage('theme', 'light')
</script>
<template>
<p>Pointer at {{ x }}, {{ y }}</p>
<select v-model="theme">
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</template>
theme is a ref. Changing it through v-model writes to localStorage; reloading the page reads it back. That’s the useLocalStorage you would otherwise write by hand: a ref, a watch that serializes on change, and an initial read.
The current major version of @vueuse/core lists Vue ^3.5.0 as its peer dependency. On an older Vue you’d need an older VueUse release. In Nuxt, the @vueuse/nuxt module auto-imports the functions so you can skip the import line.
The conventions every function shares
The value of VueUse is less any single function than the fact that all of them behave the same way. Learn the conventions once and every new function is predictable. These are the same conventions the composables lesson recommends for your own code.
Arguments can be reactive. Most parameters are typed MaybeRefOrGetter<T>, so you can pass a plain value, a ref, or a getter:
import { useTitle } from '@vueuse/core'
useTitle('Inbox') // static
useTitle(() => `Inbox (${unread.value})`) // updates when unread changes
Results are refs. Functions return a ref, or an object of refs you can destructure, so results drop straight into templates, computed, and watch.
Cleanup is automatic. Anything a function sets up (an event listener, a ResizeObserver, an interval) is torn down when the calling scope is disposed, which for a component means unmount. Many functions also return a stop function, or pause and resume, for ending things earlier.
Browser globals are configurable. Functions that touch window or document accept an option to supply your own, and they guard against those globals being missing during server rendering.
The functions you’ll reach for first
The docs group functions into categories. A handful cover most day-to-day needs.
Events and elements
<script setup>
import { ref, useTemplateRef } from 'vue'
import { onClickOutside, useEventListener, useElementSize } from '@vueuse/core'
const open = ref(false)
const menu = useTemplateRef('menu')
onClickOutside(menu, () => (open.value = false))
useEventListener(document, 'keydown', (e) => {
if (e.key === 'Escape') open.value = false
})
const { width } = useElementSize(menu)
</script>
<template>
<button @click="open = !open">Menu</button>
<div v-if="open" ref="menu">Menu is {{ Math.round(width) }}px wide</div>
</template>
onClickOutside closes the menu on any click outside the element. useEventListener adds a listener and removes it on unmount. useElementSize wraps a ResizeObserver. All three accept a template ref, and they handle the ref being null while the element isn’t rendered.
Persisted state
useStorage(key, default, storage?) is the general form; useLocalStorage and useSessionStorage are shortcuts. Objects and arrays are serialized as JSON for you, and by default (listenToStorageChanges: true) the ref updates when another tab writes the same key.
Timing
import { ref } from 'vue'
import { refDebounced, useDebounceFn } from '@vueuse/core'
const query = ref('')
const debouncedQuery = refDebounced(query, 300) // read-only ref, lags 300ms behind
const save = useDebounceFn(() => api.save(form.value), 500)
refDebounced gives you a second ref that follows the first after a pause. useDebounceFn wraps a function instead. (The debounce and throttle lesson covers how debouncing itself works.)
Watching helpers
whenever(source, cb) is a watch that only fires when the source becomes truthy. until(ref).toBe(value) returns a promise you can await until a ref reaches a value. Both appear in the video.
Around the core package
@vueuse/componentsexposes some functions as renderless components and directives, for example anOnClickOutsidecomponent and avOnClickOutsidedirective, for when you’d rather declare behavior in the template. The custom directives lesson explains how directives like that are built.@vueuse/integrationswraps third-party libraries (focus trapping, cookies, form validation helpers) in the same composable style.@vueuse/routerand others add composables for specific parts of the ecosystem.
When to write your own
VueUse is for generic logic about the browser and about Vue itself. Your app’s domain logic (useCart, useCurrentUser, usePermissions) still belongs in your own composables, and those composables will often be built from VueUse functions. A useCart might use useLocalStorage for persistence and refDebounced to batch quantity changes.
Two more reasons to write your own: when a VueUse function covers ten options and you need one, three lines of your own may be clearer than learning its API. And when you need to understand how a function behaves, its source is short and readable. Reading useStorage or useEventListener on GitHub is a good education in writing composables that hold up in production.
What the video covers
Alexander Lichter opens with a short “What is VueUse?” segment, then demonstrates functions most people haven’t tried: syncRef, useRefHistory for undo and redo, whenever and until, useScrollLock, onStartTyping, onClickOutside, useNow, useTimeAgo, and useLastChanged. Each one shows the conventions above in action. The video skips the everyday functions this lesson covers first (useMouse, the storage functions, useEventListener, debouncing), so the two complement each other. For the author’s own walkthrough, see Anthony Fu’s Vue.js Forge session under Go deeper.
Where this fits
Composables reuse logic. Sometimes the reusable thing is low-level DOM behavior attached to a single element, and Vue has a separate tool for that: custom directives.
Check yourself
Why does adding @vueuse/core to a project not add a few hundred functions' worth of code to the bundle?
VueUse is published as ES modules with each function exported separately, so a bundler doing tree-shaking only includes the functions you actually import, plus whatever internal helpers those functions use. Importing useMouse pulls in useMouse, not useStorage.
A component calls useEventListener(window, 'resize', onResize) and is then unmounted. What happens to the listener, and why?
It is removed. useEventListener registers its cleanup with the current effect scope, which for a component is disposed on unmount. That is the same automatic-cleanup mechanism a hand-written composable gets from onScopeDispose or onUnmounted, already wired up for you.
You pass a ref to useTitle and later change the ref's value. Does the page title change? What if you pass a getter instead?
Yes in both cases. VueUse functions accept MaybeRefOrGetter arguments and read them reactively, so a ref or a getter whose dependencies change both cause the document title to update. Passing a plain string sets the title once.
When should you write your own composable rather than reach for VueUse?
When the logic belongs to your domain (a useCart, a usePermissions) rather than to the browser or to Vue in general, or when a VueUse function does far more than you need and a few lines of your own would be clearer. VueUse covers generic, reusable building blocks; your business logic still lives in your own composables, often built on top of VueUse functions.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.