onCleanup
Register a cleanup function which is called when the current reactive scope is recalculated or disposed. This is useful to clean up any side effects created in the reactive scope.
import { onCleanup } from "@alloy-js/core";
function onCleanup(fn: Disposable): void;
Parameters
fn |
|
Returns
void
Remarks
When onCleanup is called inside a component definition, the provided function
is called when the component is removed from the tree. This can be useful to
clean up any side effects created as a result of rendering the component. For
example, if rendering a component creates a symbol, onCleanup
can be used
to remove the symbol when the component is removed from the tree.
When onCleanup is called inside a memo or effect, the function is called when the effect is refreshed (e.g. when a memo or computed recalculates) or disposed (e.g. it is no longer needed because it is attached to a component which was removed).