Reference

UI framework equivalents

Compare native component, state, event and lifecycle syntax in four frameworks.

Scope and conventions

React function components, Svelte 5 runes, modern SwiftUI (Observation examples require iOS 17+) and Jetpack Compose. Snippets are independent component/body fragments; import the named framework APIs. Native lifecycle and ownership rules differ.

Effects & async work

Synchronize with an external system

React

useEffect runs after commit in the client. Dependencies must include reactive values used by the effect; cleanup runs before a changed setup and on unmount.

Fragment

Before you use this

React TypeScript/JSX project; use body fragments in a component and supply app-owned names.

import { useEffect } from "react";
React · Synchronize with an external system
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
useEffect(() => {
  const unsubscribe = store.subscribe(onValue);
  return unsubscribe;
}, [store, onValue]);

Development Strict Mode can run an extra setup/cleanup cycle.

To read the latest callback without re-subscribing, wrap it with useEffectEvent (React 19.2+).

Start cancellable work for an input

React

Effects cannot themselves be async callbacks. Start the task inside and abort/ignore its result on cleanup.

Fragment

Before you use this

React TypeScript/JSX project; use body fragments in a component and supply app-owned names.

import { useEffect } from "react";
React · Start cancellable work for an input
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
useEffect(() => {
  const controller = new AbortController();
  void load(id, controller.signal).catch(handleError);
  return () => controller.abort();
}, [id, load, handleError]);

load must respect cancellation and avoid publishing stale results.

Launch async work from an action

React

An event can start a promise task; retain its error and cancellation ownership.

Fragment

Before you use this

React TypeScript/JSX project; use body fragments in a component and supply app-owned names.

React · Launch async work from an action
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
<button onClick={() => void save().catch(showError)}>
  Save
</button>

Render loading, failure and ready states

React

Represent async state explicitly; do not equate an empty value with loading.

Fragment

Before you use this

React TypeScript/JSX project; use body fragments in a component and supply app-owned names.

React · Render loading, failure and ready states
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
if (state.kind === "loading") return <Spinner />;
if (state.kind === "error") return <p>{state.message}</p>;
return <Results items={state.items} />;