---
title: "Surface lifecycle"
description: "useAppContext().lifecycle owns resources for one surface mount. Cleanup runs when the mount unmounts or its exact runtime identity changes."
url: "https://sotaagents.ai/manual/developer-guide/surface-lifecycle"
generated_by: "sotaagents-ldp"
docs_index: "https://sotaagents.ai/manual/llms.txt"
locale: "en"
---

# Surface lifecycle

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

| API | Use |
| --- | --- |
| `signal` | An `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.
