Documentation
Open app

Surface lifecycle

useAppContext().lifecycle owns resources for one surface mount. Cleanup runs when the mount unmounts or its exact runtime identity changes.

APIUse
signalAn AbortSignal aborted on unmount.
onDispose(dispose)Registers app cleanup and returns a function that unregisters it.
listen(target, type, listener, options?)Adds an event listener owned by this mount and returns its disposer.
TSX
import { useEffect, useState } from 'react';
import { useAppContext } from '@sota/platform';

export function OnlineState() {
  const { lifecycle } = useAppContext();
  const [online, setOnline] = useState(navigator.onLine);
  useEffect(() => {
    const update = () => setOnline(navigator.onLine);
    const stopOnline = lifecycle.listen(window, 'online', update);
    const stopOffline = lifecycle.listen(window, 'offline', update);
    return () => {
      stopOnline();
      stopOffline();
    };
  }, [lifecycle]);
  return <p>{online ? 'Online' : 'Offline'}</p>;
}
TypeScript
import { useEffect } from 'react';
import { useAppContext } from '@sota/platform';

export function MountTimer() {
  const { lifecycle } = useAppContext();
  useEffect(() => {
    const timer = window.setInterval(() => console.log('tick'), 30_000);
    const unregister = lifecycle.onDispose(() => window.clearInterval(timer));
    return () => {
      window.clearInterval(timer);
      unregister();
    };
  }, [lifecycle]);
  return null;
}

Use these primitives instead of process-global listeners or timers. React cleanup may call returned disposers early; Core still guarantees forced cleanup when the surface disappears.

Contents

Esc

Search titles and body text across every chapter.