Vue 3 cheat codes we actually use in production

Not the Vue 3 tutorial. The handful of patterns that show up in almost every Vankode Vue codebase because they save hours, prevent bugs, or make refactors survivable.

AI-generated editorial scene of the Vue symbol in a jade and ivory courtyard beside a reflecting pool

This is not a Vue 3 tutorial — go read the Vue docs for that. This is the short list of patterns that show up in almost every Vankode Vue codebase because they save real time.

1. defineModel kills v-model boilerplate

Before Vue 3.4:

<script setup lang="ts">
const props = defineProps<{ modelValue: string }>()
const emit = defineEmits<{ 'update:modelValue': [value: string] }>()

const local = computed({
    get: () => props.modelValue,
    set: (v) => emit('update:modelValue', v)
})
</script>

<template>
    <input v-model="local" />
</template>

After:

<script setup lang="ts">
const model = defineModel<string>()
</script>

<template>
    <input v-model="model" />
</template>

That's five lines to one. See the defineModel docs. We use it on every custom input.

2. shallowRef for anything not deeply reactive

If you are holding a Map, Set, class instance, or third-party object in state, use shallowRef. It skips the deep reactivity proxy and is dramatically faster.

import { shallowRef } from 'vue'

const lenis = shallowRef<Lenis | null>(null)
const gsapTimeline = shallowRef<gsap.core.Timeline | null>(null)

The rule of thumb: if you never mutate the object's internals through Vue, use shallowRef.

3. Typed props with withDefaults

TypeScript-safe defaults without a runtime schema:

interface Props {
    severity?: 'info' | 'warn' | 'crit'
    title?: string
}

const props = withDefaults(defineProps<Props>(), {
    severity: 'info'
})

Now props.severity is a plain string union in the template — no ?? 'info' fallback needed.

4. effectScope for composables that own multiple listeners

When a composable attaches several watch / watchEffect / DOM listeners, wrap them in a scope so you can dispose them together:

import { effectScope, onScopeDispose, watch } from 'vue'

export function useResizeObserver(target: Ref<HTMLElement | null>) {
    const scope = effectScope()

    scope.run(() => {
        watch(target, (el) => {
            if (!el) return
            const ro = new ResizeObserver(() => { /* ... */ })
            ro.observe(el)
            onScopeDispose(() => ro.disconnect())
        })
    })

    onScopeDispose(() => scope.stop())
}

See effectScope docs.

5. provide / inject with a typed key

Global-ish state without Pinia, without any:

// composables/useTheme.ts
import { type InjectionKey, inject, provide, ref } from 'vue'

interface ThemeApi {
    mode: Ref<'light' | 'dark'>
    toggle: () => void
}

export const THEME_KEY: InjectionKey<ThemeApi> = Symbol('theme')

export function provideTheme() {
    const mode = ref<'light' | 'dark'>('light')
    const api: ThemeApi = {
        mode,
        toggle: () => { mode.value = mode.value === 'light' ? 'dark' : 'light' }
    }
    provide(THEME_KEY, api)
    return api
}

export function useTheme(): ThemeApi {
    const api = inject(THEME_KEY)
    if (!api) throw new Error('useTheme called without provideTheme parent')
    return api
}

The InjectionKey<T> gives you full type inference on the consumer side. Docs.

6. <Suspense> + useAsyncData for page-level loads

In Nuxt, every await useAsyncData(...) inside <script setup> is wrapped in an implicit <Suspense>. You can exploit that:

<script setup lang="ts">
const { data: posts } = await useAsyncData('log4j', () =>
    queryCollection('log4j')
        .where('draft', '=', false)
        .order('date', 'DESC')
        .all()
)
</script>

<template>
    <Log4jFeed :posts="posts ?? []" />
</template>

No v-if="posts" guards, no loading state scattered across children. Docs.

7. readonly for exposed refs

When a composable returns reactive state, wrap it in readonly so consumers cannot mutate it directly:

import { ref, readonly } from 'vue'

export function useCounter() {
    const count = ref(0)
    const increment = () => { count.value++ }
    return {
        count: readonly(count),
        increment
    }
}

You keep the one-way-data-flow invariant without discipline. Docs.

8. Teleport for modals and overlays

<template>
    <Teleport to="body">
        <div class="dialog" role="dialog">
            <slot />
        </div>
    </Teleport>
</template>

Your modal stops inheriting overflow: hidden / transform / z-index parent constraints. One line, huge UX win. Docs.

These eight patterns cover 80% of the "how should I do this in Vue 3?" questions we get from new hires. Save this page.