0
0
Vueframework~10 mins

Composable with lifecycle hooks in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Composable with lifecycle hooks
Start Component Setup
Call Composable Function
Inside Composable: Setup Reactive State
Register Lifecycle Hook (e.g., onMounted)
Return State and Methods
Component Uses Returned State
Component Mounts
Lifecycle Hook Runs
Effect Happens (e.g., console log, data fetch)
Component Renders with Updated State
The component calls a composable that sets up reactive state and registers lifecycle hooks. When the component mounts, the lifecycle hook inside the composable runs, triggering side effects.
Execution Sample
Vue
import { ref, onMounted } from 'vue'

function useCounter() {
  const count = ref(0)
  onMounted(() => {
    console.log('Mounted!')
  })
  return { count }
}
This composable creates a reactive count and logs 'Mounted!' when the component using it mounts.
Execution Table
StepActionState BeforeState AfterEffect
1Component setup startsNo stateNo stateNo effect
2Call useCounter composableNo statecount = 0No effect
3Register onMounted hookcount = 0count = 0Hook registered
4Composable returns { count }count = 0count = 0No effect
5Component mountscount = 0count = 0Trigger onMounted hook
6onMounted hook runscount = 0count = 0Console logs 'Mounted!'
7Component renders with count=0count = 0count = 0UI shows count 0
💡 Lifecycle hook runs once on component mount, then normal rendering continues.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 6Final
countundefined0000
Key Moments - 2 Insights
Why does the console.log inside onMounted run only after the component mounts?
Because onMounted registers a hook that runs only when the component finishes mounting, as shown in step 5 and 6 of the execution_table.
Is the reactive state 'count' available before the component mounts?
Yes, 'count' is created during composable call in setup (step 2), but lifecycle hooks run later (step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'count' after step 3?
A1
Bundefined
C0
Dnull
💡 Hint
Check the 'State After' column for step 3 in execution_table.
At which step does the onMounted hook actually run?
AStep 2
BStep 6
CStep 4
DStep 7
💡 Hint
Look for the step where the effect 'Console logs "Mounted!"' happens in execution_table.
If we add a reactive variable inside onMounted instead of setup, when will it be available?
AOnly after component mounts
BImmediately during setup
CBefore component setup
DNever
💡 Hint
Recall that onMounted runs after mounting, so variables created there appear later.
Concept Snapshot
Composable with lifecycle hooks in Vue:
- Define reactive state inside composable using ref/reactive.
- Register lifecycle hooks like onMounted inside composable.
- Return state and methods from composable.
- Hooks run when component lifecycle reaches that phase.
- Enables reusable logic with side effects tied to component lifecycle.
Full Transcript
In Vue, a composable is a function that sets up reactive state and can register lifecycle hooks like onMounted. When a component calls this composable during its setup phase, the reactive state is created immediately. However, lifecycle hooks registered inside the composable only run when the component reaches that lifecycle phase, for example, when it mounts. This allows the composable to perform side effects such as logging or fetching data exactly when the component is ready. The execution table shows the step-by-step process: the component starts setup, calls the composable which creates state and registers hooks, then when the component mounts, the onMounted hook runs and triggers effects. This pattern helps organize reusable logic with lifecycle awareness.