onCleanup
Edit this pageonCleanup
registers a cleanup method that executes on disposal and recalculation of the current reactive scope.
Can be used anywhere to clean up any side effects left behind by initialization.
When used in a Component, it runs when the component is unmounted.
When used in reactive contexts, such createEffect
, createMemo
or a createRoot
, it runs when the reactive scope is disposed or refreshed.
Without the onCleanup
function, the event listener would remain attached to the document
even after the component is removed from the page.
This can cause memory leaks and other issues.