Skip to main content
Schedules a callback to run immediately before the component is unmounted. Of the lifecycle functions (onMount, beforeUpdate, afterUpdate and onDestroy), this is the only one that runs inside a server-side component.

Signature

fn
function
required
A function to run immediately before the component is unmounted.

Usage

Cleanup example

With external modules

onDestroy can be called from external modules, making it useful for encapsulating reusable logic:

Server-side rendering

Unlike other lifecycle functions, onDestroy does run during server-side rendering. This makes it useful for cleanup that should happen in both client and server environments.

Notes

  • Must be called during component initialization
  • Runs on both client and server
  • Can be called from external modules
  • Runs immediately before the component is unmounted