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.

Events & input

Handle a button action

React

Pass a function; do not call it during rendering.

Fragment

Before you use this

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

React · Handle a button action
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
<button onClick={() => save()}>Save</button>

Connect a text field to state

React

Controlled input state supplies value and handles changes.

Fragment

Before you use this

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

React · Connect a text field to state
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
<input
  value={name}
  onChange={event => setName(event.target.value)}
/>

Let a child request a parent update

React

Receive a callback prop; the parent owns the state transition.

Fragment

Before you use this

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

React · Let a child request a parent update
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
function Remove({ onRemove }: { onRemove: () => void }) {
  return <button onClick={onRemove}>Remove</button>;
}

Expose writable state intentionally

React

Use an explicit controlled value/change callback pair. React has no built-in two-way prop binding.

Fragment

Before you use this

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

React · Expose writable state intentionally
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
<Editor value={draft} onChange={setDraft} />

Submit input through the platform’s event model

React

An HTML form submits by default. Prevent that navigation when a client handler owns submission; specify button types.

Fragment

Before you use this

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

React · Submit input through the platform’s event model
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
<form onSubmit={event => { event.preventDefault(); save(); }}>
  <input name="name" required />
  <button type="submit">Save</button>
</form>

Handle async rejection and validate on the server as well as in the browser.

React 19 also accepts a function as <form action={...}>: it receives FormData, needs no preventDefault and works with useActionState and useFormStatus.