Question presented to candidate: "Walk me through the lifecycle of a Vue 3 component. When does each hook run, in what order do a parent and its child fire, and where would you put a data fetch, a DOM measurement and a cleanup?"
What a strong answer should cover:
- The phases: setup (or
beforeCreate/created), mount (onBeforeMount,onMounted), update (onBeforeUpdate,onUpdated) and unmount (onBeforeUnmount,onUnmounted). - Order between parent and child: parent setup and beforeMount, then the child completes its mount, then the parent's
onMounted; unmount runs the parent'sonBeforeUnmountfirst and itsonUnmountedlast. - In the Composition API,
setup()replacesbeforeCreateandcreatedand runs before both. - Hooks must be registered synchronously during setup; a late registration logs a warning and never runs.
- SSR runs only setup (and
onServerPrefetch); mount, update and unmount hooks never run on the server. - The extra hooks:
onActivated/onDeactivated(KeepAlive),onErrorCaptured, and the dev-onlyonRenderTracked/onRenderTriggered.
Clarifying questions expected:
- "Composition API or Options API names? They map one to one, but the vocabulary differs."
- "Does the app use SSR or KeepAlive? Both change which hooks run."
Code / implementation expected: Optional β a parent and child that log each hook, to prove the order rather than recite it.