[]previous and next page

onCleanup

onCleanup(fn) registers fn on the current owner.

For agentsUpdated

onCleanup(fn) registers fn on the current owner. It runs when that 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. Called with no owner, onCleanup warns NO_OWNER_CLEANUP and the function never runs.

Use it in a component body for resources the component holds (timers, listeners, subscriptions):

src/clock.tsxtsx
import { onCleanup } from "@shadow-js/core";

export function Clock() {
  const timer = setInterval(() => {}, 1000);
  onCleanup(() => clearInterval(timer));
  return <span>tick</span>;
}