useLazy
A composable for deferring content rendering until first activation, with optional reset on deactivation.
Usage
The useLazy composable tracks whether content has been activated at least once. Content renders only after first activation (unless eager mode is enabled), reducing initial render cost for components like dialogs, menus, and tooltips.
import { shallowRef } from 'vue'
import { useLazy } from '@vuetify/v0'
const isOpen = shallowRef(false)
const { isBooted, hasContent, onAfterLeave } = useLazy(isOpen)
// hasContent becomes true after isOpen is first set to true
// onAfterLeave resets lazy state for transition integrationArchitecture
Reactivity
| Property/Method | Reactive | Notes |
|---|---|---|
isBooted | ShallowRef, readonly | |
hasContent | Computed from isBooted || eager || active | |
active | Accepts MaybeRefOrGetter, watched for changes | |
eager | Accepts MaybeRefOrGetter in options |
Examples
Overview row 1
Overview row 2
Overview row 3
Overview row 4
Overview row 5
Overview row 6
isBooted: true · hasContent: true
Panel mounts
Overview: 0 mount(s)
Analytics: 0 mount(s)
Settings: 0 mount(s)
Switch tabs and back — each panel mounts once, then stays.
Recipes
Delay
Use the delay option to defer the first mount by a fixed number of milliseconds. This prevents a flash of content for operations that complete very quickly:
const { hasContent } = useLazy(isOpen, { delay: 200 })
// Content only mounts if isOpen stays true for 200msEager Mode
Use the eager option to render content immediately without waiting for activation:
const { hasContent } = useLazy(isOpen, { eager: true })
// hasContent.value is always trueThe eager option accepts a reactive value for dynamic control:
const props = defineProps<{ eager: boolean }>()
const { hasContent } = useLazy(isOpen, {
eager: toRef(() => props.eager),
})Transition Integration
The onAfterLeave callback resets the lazy state after the leave transition completes (unless eager mode is enabled):
<template>
<Transition @after-leave="onAfterLeave">
<div v-if="isOpen">
<template v-if="hasContent">
<!-- Heavy content -->
</template>
</div>
</Transition>
</template>This allows memory to be reclaimed when the content is hidden, while preserving the content during the leave animation.
FAQ
useLazy defers a subtree’s first mount until it’s activated, and can tear it back down later. usePresence orchestrates enter and leave animations for content that is already mounting. Reach for useLazy to skip render cost, usePresence to animate transitions.
Wire onAfterLeave into a Transition’s @after-leave. It resets isBooted once the leave animation finishes, so the subtree unmounts — at the cost of re-mounting on the next open. Omit it to keep content alive after first boot.
Yes — pass { eager: true } and hasContent is always true. eager also accepts a ref or getter, so you can drive lazy behavior from a prop.