All lifecycle hooks are automatically bound to the current active instance, so you can access component state, computed properties, and methods.
Type Signature
Registration
Lifecycle hooks should be registered synchronously duringsetup() or <script setup>:
onBeforeMount()
Called right before the component is to be mounted. Type Signature:Example
Timing
- Called after
setup()completes - Called before the initial render
- Component’s reactive state is set up
- DOM has not been created yet
onMounted()
Called after the component has been mounted. Type Signature:Example
Timing
- Called after:
- All synchronous child components have been mounted
- Its own DOM tree has been created and inserted into the parent container
- Use this hook to:
- Access DOM elements
- Initialize third-party libraries
- Start data fetching
This hook is not called during server-side rendering.
onBeforeUpdate()
Called right before the component is about to update its DOM tree due to a reactive state change. Type Signature:Example
Use Cases
- Access DOM state before Vue updates it
- Useful for removing manually added event listeners
This hook is not called during server-side rendering.
onUpdated()
Called after the component has updated its DOM tree due to a reactive state change. Type Signature:Example
Timing
- Called after any DOM update caused by reactive state changes
- Parent component’s
onUpdatedis called after child components
This hook is not called during server-side rendering.
onBeforeUnmount()
Called right before a component instance is to be unmounted. Type Signature:Example
Timing
- Component instance is still fully functional
- Good place to clean up manually created side effects like timers or server connections
This hook is not called during server-side rendering.
onUnmounted()
Called after the component has been unmounted. Type Signature:Example
Timing
- Called after:
- All child components have been unmounted
- All associated reactive effects have been stopped
Use Cases
- Clean up timers
- Cancel network requests
- Remove event listeners
- Tear down integrations
This hook is not called during server-side rendering.
onActivated()
Called when a kept-alive component instance is activated (inserted into the DOM). Type Signature:Example
Only applies to components wrapped in
<KeepAlive>.onDeactivated()
Called when a kept-alive component instance is deactivated (removed from the DOM). Type Signature:Example
Only applies to components wrapped in
<KeepAlive>.onErrorCaptured()
Called when an error propagating from a descendant component has been captured. Type Signature:Parameters
The error object
The component instance that triggered the error
A string specifying the error source type
Example
Error Sources
- Component renders
- Event handlers
- Lifecycle hooks
setup()function- Watchers
- Custom directive hooks
- Transition hooks
Return Value
- Return
falseto stop the error from propagating further - By default, errors propagate to
app.config.errorHandler
onRenderTracked()
Called when a reactive dependency has been tracked by the component’s render effect. Type Signature:Example
This hook is development-only and not called during server-side rendering.
onRenderTriggered()
Called when a reactive dependency triggers the component’s render effect to re-run. Type Signature:Example
This hook is development-only and not called during server-side rendering.
onServerPrefetch()
Registers an async function to be resolved before the component instance is rendered on the server. Type Signature:Example
Use Cases
- Pre-fetch data during SSR
- Perform async operations before server rendering
- The returned promise will be awaited before rendering
This hook is only called during server-side rendering.