Before and after: migrating a Vue pattern without breaking everything

Migrating from the Options API to Composition API across a real codebase is not a weekend job. Here is the boring, staged approach that got us from one to the other with zero production incidents.

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

A real migration from Options API to Composition API, stepped out so you can see where the risk actually lives.

Before: the Options API component

<script>
import { debounce } from 'lodash-es'
import { mapState, mapActions } from 'pinia'
import { usePostStore } from '@/stores/posts'

export default {
    name: 'PostSearch',
    props: {
        category: { type: String, required: true }
    },
    data() {
        return {
            query: '',
            showResults: false
        }
    },
    computed: {
        ...mapState(usePostStore, ['posts', 'loading']),
        results() {
            return this.posts
                .filter((p) => p.category === this.category)
                .filter((p) => p.title.toLowerCase().includes(this.query.toLowerCase()))
        }
    },
    watch: {
        query: {
            handler: 'search',
            immediate: false
        }
    },
    methods: {
        ...mapActions(usePostStore, ['fetchPosts']),
        search: debounce(function () {
            this.showResults = this.query.length > 2
        }, 250)
    },
    mounted() {
        this.fetchPosts()
    }
}
</script>

<template>
    <div class="post-search">
        <input v-model="query" placeholder="Search posts" />
        <ul v-if="showResults && !loading">
            <li v-for="post in results" :key="post.id">{{ post.title }}</li>
        </ul>
    </div>
</template>

After: Composition API with <script setup>

<script setup lang="ts">
import { ref, computed, watch, onMounted } from 'vue'
import { storeToRefs } from 'pinia'
import { useDebounceFn } from '@vueuse/core'
import { usePostStore } from '@/stores/posts'

const props = defineProps<{ category: string }>()

const query = ref('')
const showResults = ref(false)

const postStore = usePostStore()
const { posts, loading } = storeToRefs(postStore)

const results = computed(() =>
    posts.value
        .filter((p) => p.category === props.category)
        .filter((p) => p.title.toLowerCase().includes(query.value.toLowerCase()))
)

const search = useDebounceFn(() => {
    showResults.value = query.value.length > 2
}, 250)

watch(query, () => search())

onMounted(() => postStore.fetchPosts())
</script>

<template>
    <div class="post-search">
        <input v-model="query" placeholder="Search posts" />
        <ul v-if="showResults && !loading">
            <li v-for="post in results" :key="post.id">{{ post.title }}</li>
        </ul>
    </div>
</template>

The migration checklist

We do not try to convert in one edit. We go in four passes, one PR each.

Pass 1: type-annotate the props

// before
props: { category: String }

// after
props: { category: { type: String, required: true } }

Then add a lang="ts" to <script>. This is a no-behaviour-change PR but it tightens the types and surfaces anything that was silently any.

Pass 2: replace Vuex/legacy Pinia bindings

Switch from mapState / mapActions to explicit useStore calls. Still inside the Options API. Small, mechanical, reviewable.

Pass 3: flip to <script setup>

Now do the actual API flip. Since props, store, and types are already explicit, this step is mostly renames:

  • data() → ref()
  • computed → computed()
  • methods → plain functions
  • watch → watch()
  • mounted → onMounted()

If a component has multiple lifecycle hooks or complex reactivity, split it into composables at this step.

Pass 4: extract composables

Any logic that is reusable — a debounced search, a resize observer, a keyboard shortcut handler — becomes its own composables/useXxx.ts. Tests get easier. Reusability gets real.

Things that bit us

  • this-bound debounce. The lodash debounce with a function() needs this. useDebounceFn does not. Easy mistake.
  • Pinia store reactivity. Destructuring a store loses reactivity. Always use storeToRefs.
  • Watchers running immediately. Options API watch with immediate: true runs before onMounted. Composition API watchEffect behaves similarly but not identically.
  • Template refs. In <script setup>, a ref() in script is a template ref if the ref="..." attribute in the template matches by name.

The rule of thumb

Do not do a big bang migration. Convert the surface-level API first, stabilise, commit, and then go deeper. Four small PRs beat one enormous one every time.