vuejs
/

Vue Lifecycle Hooks

Last Sync: Today

On this page

10
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

vuejs

Vue Lifecycle Hooks

What are Lifecycle Hooks?

Lifecycle hooks are special methods in Vue that allow you to run code at specific stages of a component's life.

Lifecycle Phases

PhaseHooks
Creationcreated
MountingbeforeMount, mounted
UpdatingbeforeUpdate, updated
UnmountingbeforeUnmount, unmounted

created Hook

Called when the component is created but not yet mounted to the DOM.

JavaScriptRead-only
1
created() {
  console.log('Component created');
}

mounted Hook

Called after the component is mounted to the DOM.

JavaScriptRead-only
1
mounted() {
  console.log('Component mounted');
}

updated Hook

Called when reactive data changes and the DOM updates.

JavaScriptRead-only
1
updated() {
  console.log('Component updated');
}

unmounted Hook

Called when the component is removed from the DOM.

JavaScriptRead-only
1
unmounted() {
  console.log('Component destroyed');
}

Use Cases

  • Fetch data in mounted()
  • Initialize variables in created()
  • Watch DOM updates in updated()
  • Cleanup resources in unmounted()

Best Practices

  • Use mounted for DOM-related logic
  • Avoid heavy logic in updated
  • Clean up timers and listeners in unmounted
  • Use created for initial setup

Common Mistakes

  • Using DOM before mounted
  • Heavy operations in updated hook
  • Not cleaning resources in unmounted
  • Confusing created and mounted hooks

Conclusion

Lifecycle hooks are essential for controlling component behavior at different stages, enabling efficient and organized Vue applications.

Try it yourself

mounted() {
  console.log('Mounted');
}

Test Your Knowledge

Q1
of 3

Which hook runs after DOM?

A
created
B
mounted
C
updated
D
beforeCreate
Q2
of 3

Cleanup happens in?

A
created
B
mounted
C
unmounted
D
updated
Q3
of 3

First hook?

A
mounted
B
created
C
updated
D
unmounted

Frequently Asked Questions

What is mounted?

It runs after component is added to DOM.

When to fetch API?

In mounted hook.

What is unmounted?

It runs when component is removed.

Previous

vue dynamic components

Next

vue router intro

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.