Chapter 42: Vue Composition API

Vue Composition API — the single biggest change and improvement that happened in Vue 3.

I’m going to explain it like I’m sitting next to you, pair-programming, showing you why almost every serious Vue developer in 2026 uses it instead of the old Options API.

1. What problem does Composition API solve? (The honest reason it exists)

Imagine you’re building a medium/large component in the old Options API style:

JavaScript

After 200–300 lines it becomes very hard to:

  • See which data/methods/computeds/watchers belong together logically
  • Reuse logic between components (you end up with mixins → nightmare)
  • Extract a piece of logic into its own file
  • Type it properly with TypeScript

Composition API fixes exactly this by letting you organize code by logical concern instead of by type.

You group related:

  • state (ref/reactive)
  • computed values
  • methods/functions
  • watchers
  • lifecycle hooks
  • composables (reusable logic blocks)

…all together in one place — usually inside <script setup>.

2. The Heart: <script setup> (2026 standard – 95%+ of new code)

This is the modern, cleanest way to use Composition API.

vue

No export default { }No thisNo separation by data/computed/methods/watch → Everything is just normal JavaScript at the top level

3. Core Building Blocks of Composition API

Concept Old Options API Composition API (script setup) Why it’s better
Reactive state data() { return { count: 0 } } const count = ref(0) No this, explicit reactivity
Reactive objects data() { user: {} } const user = reactive({ name: ”, age: 0 }) Deep reactivity without .value
Computed computed: { double() { … } } const double = computed(() => count.value * 2) Normal function, auto .value in template
Watch watch: { count(new) { … } } watch(count, (newVal, oldVal) => { … }) More flexible, can watch getters / arrays
Lifecycle mounted() { … } onMounted(() => { … }) Imported functions – can be used anywhere
Props props: { name: String } const props = defineProps<{ name: string }>() Type-safe, no this
Emits emits: [‘update’] const emit = defineEmits<{ (e: ‘update’): void }>() Type-safe events

4. Real, Complete Example – User Profile Form (shows grouping by concern)

vue

Why Composition API Wins in 2026 (Summary Table)

Problem / Pain Point Options API (old) Composition API (<script setup>) Winner
Logical grouping Separated by type → hard to follow related code Grouped by feature → very readable Composition
Code reuse Mixins → naming conflicts, hard to trace Composables → clean, importable functions Composition
TypeScript experience Painful (this.$refs, this.$emit…) Excellent – normal JS + defineProps/Emits Composition
Large components (200+ lines) Becomes spaghetti Still readable – logic stays together Composition
Extracting logic to separate file Mixins or very awkward Just make a composable → useCounter() Composition
Boilerplate Lots (export default, methods:, etc.) Minimal Composition

Quick Rules of Thumb (2026)

  • New project → always use <script setup> + Composition API
  • Legacy / old tutorials → you will see Options API — learn to read it, but write Composition
  • Need to share logic? → make a composable (useAuth, useForm, useFetch)
  • Need global state? → Pinia (not provide/inject for most cases)
  • Keep composables pure & testable — no DOM access inside them

Your next mini-project after this lesson:

  • Convert any Options API component you find online to <script setup>
  • Extract counter logic + validation into useCounter.js composable
  • Use it in two different components

Any part still confusing? Want to see:

  • Full useForm composable example with validation?
  • Composition API + Pinia + router in one project?
  • How to gradually migrate old Options API code?
  • Composition vs Options side-by-side comparison?

Just tell me — we’ll build the next clean Composition API feature together 🚀

Happy composing from Hyderabad! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *