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.

State & derivation

Own local observable state

React

useState retains state across renders for this component identity.

Fragment

Before you use this

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

import { useState } from "react";
React · Own local observable state
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const [count, setCount] = useState(0);

Update using the current value

React

A functional updater receives the queued state value; it avoids using a stale render snapshot.

Fragment

Before you use this

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

React · Update using the current value
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
setCount(previous => previous + 1);

Compute a value from state

React

Calculate inexpensive derived values during render. Store the source state rather than a second synchronized copy.

Fragment

Before you use this

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

React · Compute a value from state
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const total = price * quantity;

Update collection state

React

Create a new collection rather than mutating the state value in place.

Fragment

Before you use this

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

React · Update collection state
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
setItems(previous => [...previous, newItem]);

Know what survives identity or screen changes

React

State survives renders while the component keeps its position/type/key identity. A changed key resets it.

Fragment

Before you use this

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

React · Know what survives identity or screen changes
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
<Editor key={documentId} />

Observe an application-owned model or store

React

useSyncExternalStore bridges an external store with React rendering. subscribe must return cleanup; snapshots must be stable until the store changes.

Fragment

Before you use this

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

import { useSyncExternalStore } from "react";
React · Observe an application-owned model or store
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const snapshot = useSyncExternalStore(
  store.subscribe,
  store.getSnapshot,
  store.getServerSnapshot
);

The store provides these methods and appropriate server snapshots. Merely reading a mutable object does not subscribe React to it.

Edit caller-owned model fields

React

The caller owns the model and exposes an update callback; render from the latest prop.

Fragment

Before you use this

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

React · Edit caller-owned model fields
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
<NameEditor
  name={model.name}
  onNameChange={name => updateModel({ ...model, name })}
/>

Distinguish cached computation from state ownership

React

useMemo caches a calculation between relevant dependency changes. It is a performance tool, not durable storage or a side-effect boundary.

Fragment

Before you use this

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

import { useMemo } from "react";
React · Distinguish cached computation from state ownership
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const sorted = useMemo(() => sortItems(items), [items]);