Skip to main content
Version: 0.1.1

Function: onDestroy()

Imported from @react-logic/di

onDestroy(fn): void

Register a callback to run when the surrounding construction scope is torn down. Must be called synchronously during a logic class or service constructor (or as a field initializer). Throws otherwise.

For logic classes, the callback fires when the consuming component unmounts. For services constructed via DI, it fires when the providing <Injector> unmounts.

Parameters

fn

() => void

Returns

void

Example

class TimeService {
timer = state<ReturnType<typeof setInterval> | null>(null);
constructor() {
onDestroy(() => {
const t = this.timer();
if (t) clearInterval(t);
});
}
}