[]previous and next page

onCleanup

Registers fn to run when the current owner is disposed or, for an effect or memo, before it re-runs.

Registers fn to run when the current owner is disposed or, for an effect or memo, before it re-runs. Cleanups run newest first and untracked; one that throws does not stop the others, and its error goes to the nearest error handler (onError, catchError). Outside any owner the call does nothing, because nothing would ever run the cleanup; development builds warn NO_OWNER_CLEANUP.

Import#

ts
import { onCleanup } from "@shadow-js/core";

Signature#

ts
function onCleanup(fn: Cleanup): void

Parameters#

Name Description
fn The cleanup to register.

Examples#

ts
import { onCleanup } from "@shadow-js/core";

function Ticker() {
  const timer = setInterval(() => console.log("tick"), 1000);
  onCleanup(() => clearInterval(timer));
  return null;
}