Skip to content

Chapter 5 of 8

Component Lifecycle, Props, and Emits

Every Vue component goes through a series of lifecycle stages, and Vue exposes hooks that fire at key moments. In the Composition API these are functions like onBeforeMount, onMounted, onBeforeUpdate, onUpdated, onBeforeUnmount, and onUnmounted. The onMounted hook fires after the component has been added to the DOM, making it the right place for tasks that require access to the rendered DOM—such as fetching initial data, integrating with third-party libraries like charts or maps, attaching event listeners, or focusing an input via a template ref. Conversely, onUnmounted runs after the component has been removed, and it is the appropriate place to clean up those listeners, clear timers, cancel in-flight requests, or close WebSocket connections to prevent memory leaks.

Component communication begins with props, which are declared in script setup using the defineProps compiler macro. defineProps accepts a schema describing each prop's type, whether it is required, and a default value if optional. On the opposite side of the data flow, child components communicate upward to their parents by emitting events. In script setup, you obtain an emitter with defineEmits(['eventName']) and then call emit('eventName', payload). The parent listens with @eventName="handler". Emits can also be declared with an object syntax that includes a validation function, and a false return value triggers a development-mode warning to help catch mistakes.

Beyond props and events, script setup components are closed by default, meaning parents cannot reach into a child to read its internal state. To selectively expose bindings, the child uses the defineExpose macro to explicitly publish properties or methods. On the parent side, a template ref can be declared with ref(null) and attached to a child component via ref="myRef", giving access to whatever the child has exposed. This pattern is essential for building imperative component APIs, such as exposing a reset or validate method from a form component.

All chapters
  1. 1Introduction to Vue and the Reactivity System
  2. 2Component API Styles: Options vs Composition
  3. 3Template Directives
  4. 4Computed Properties and Watchers
  5. 5Component Lifecycle, Props, and Emits
  6. 6Slots, Provide/Inject, and Advanced Features
  7. 7State Management with Pinia
  8. 8Routing with Vue Router

Drill it

Reading is not remembering. These come from the Vue Framework deck:

Q

What is Vue.js?

Vue.js is a progressive JavaScript framework for building user interfaces. It is designed to be incrementally adoptable and focuses on the view layer, making it...

Q

What is the Vue reactivity system?

The Vue reactivity system automatically tracks dependencies and updates the DOM when reactive state changes. In Vue 3, it uses Proxy objects (instead of Object....

Q

How do you create a reactive object in Vue 3?

Use reactive() from the Composition API:import { reactive } from 'vue'const state = reactive({ count: 0, name: 'Vue' })This returns a deeply reactive proxy of t...

Q

What is the difference between ref() and reactive()?

ref() wraps a single value (primitive or object) and requires .value to access it in script. reactive() wraps an object and provides direct property access.cons...