@fcannizzaro/streamdeck-react

Lifecycle Hooks

React hooks for Stream Deck action lifecycle events.

useWillAppear

Runs when an action appears, including when a recycled root resumes. Use this to start resources needed while the action is visible.

The event is sticky: if the hook subscribes after the appearance event, it receives the latest payload immediately.

function useWillAppear(callback: (payload: WillAppearPayload) => void): void;
interface WillAppearPayload {
  settings: JsonObject;
  controller: "Keypad" | "Encoder";
  isInMultiAction: boolean;
}

useWillDisappear

Runs when an action disappears, before its root is suspended or unmounted.

function useWillDisappear(callback: () => void): void;
function StreamingKey() {
  const connectionRef = useRef<ReturnType<typeof openConnection> | null>(null);

  useWillAppear(() => {
    connectionRef.current = openConnection();
  });

  useWillDisappear(() => {
    const connection = connectionRef.current;
    connectionRef.current = null;
    connection?.close();
  });

  // ...
}

Cleanup and recycled roots

Per-action roots enter a recycling pool when they disappear. Suspension preserves the React tree, local state, and refs. React does not run unmount effect cleanup at that point.

Use useWillDisappear to close windows and connections, abort requests, and clear timers that should stop when the action leaves the device. Use useWillAppear to restart them when it returns. An effect with an empty dependency array only runs on the initial React mount, so it does not initialize each appearance.

A recycled root can resume for a different instance of the same action type. Read the current instance ID from useAction() and persisted values from useSettings(). See per-action coordinator channels for initialization that follows the action identity.

For resources with asynchronous close callbacks, check that the ref still points to the closing resource before clearing it. A delayed callback from an old window must not clear a replacement window's ref. The native window sample demonstrates this pattern.

On this page