Skip to main content

How do hooks differ in the Options API and Composition API?

1. The main difference - where and how they are used

Options API - hooks are object methods

javascript
export default { mounted() { console.log('Component mounted') } }

Composition API - hooks are imported functions

javascript
import { onMounted } from 'vue' onMounted(() => { console.log('Component mounted') })

2. The difference in context (this)

Options API

Hooks are called inside the object -> this is available inside them:

javascript
mounted() { console.log(this.user) // works }

Composition API

There is no this context - everything is explicit:

javascript
const user = ref('Tim') onMounted(() => { console.log(user.value) // works })

Composition API removes the confusion around this.


3. The difference in code organization

Options API

Logic is spread across the object's sections:

  • data
  • methods
  • computed
  • mounted
  • watch
  • ...

If the logic consists of several parts, it gets split up and scattered across the file.

Composition API

Logic is grouped by meaning:

javascript
const isOpen = ref(false) function toggle() { isOpen.value = !isOpen.value } onMounted(() => { syncWithServer() })

Logic that belongs together sits next to each other -> the code is cleaner and easier to maintain.


4. The lifecycle is the same, but the hook names differ

Options API:

  • mounted
  • updated
  • unmounted
  • ...

Composition API:

  • onMounted
  • onUpdated
  • onUnmounted
  • ...

The meaning is 1 to 1, only the names differ.


5. In the Composition API, hooks can be called from composables

This is the main advantage of Vue 3.

You can extract the logic into a function:

javascript
export function useFetch(url) { const data = ref(null) onMounted(async () => { data.value = await fetch(url).then(r => r.json()) }) return { data } }

And use it in any component:

javascript
const { data } = useFetch('/api/users')

You cannot do this in the Options API - hooks are strictly tied to the component.


6. The Options API has 2 unique hooks

(that do not exist in the Composition API)

  • beforeCreate
  • created

Why don't they exist in the Composition API? Because setup() itself is the created phase.


7. In <script setup> all hooks automatically belong to the component

No manual setup(), just:

javascript
<script setup> onMounted(() => {}) onUnmounted(() => {}) </script>

Vue itself understands that the hooks belong to the current component.


Comparison table

CharacteristicOptions APIComposition API
How they are calledas methodsas functions
Use thisyesno
Logic groupingby type (data/methods/...)by meaning
Extracting logic (composables)limitedfully
Unique hooksbeforeCreate, creatednone, replaced by setup()
Suited forsmall componentslarge projects, complex logic
Readabilityeasier for beginnersbetter in large projects

Summary (cheat sheet)

Options API: hooks are object methods, this exists, logic is scattered. Composition API: hooks are functions, there is no this, logic is grouped by meaning, hooks can be used inside composables.

Hooks do the same thing, but they look and are organized differently.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.