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.

Components & inputs

Declare a component

React

A function returns JSX. Component names begin with a capital letter.

Fragment

Before you use this

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

React · Declare a component
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
function Greeting() {
  return <p>Hello</p>;
}

Receive an input

React

Props are inputs owned by the caller; do not mutate them.

Fragment

Before you use this

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

React · Receive an input
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
function Greeting({ name }: { name: string }) {
  return <p>Hello, {name}</p>;
}

Provide a default input

React

A destructuring default applies when the prop is undefined.

Fragment

Before you use this

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

React · Provide a default input
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
function Badge({ label = "New" }) {
  return <span>{label}</span>;
}

Place state and lifecycle APIs in supported contexts

React

Call ordinary Hooks at the top level of a component/custom Hook before conditional returns. A condition can select rendered output after the Hooks have run.

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 · Place state and lifecycle APIs in supported contexts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
function Panel({ visible }: { visible: boolean }) {
  const [count, setCount] = useState(0);
  if (!visible) return null;
  return <button onClick={() => setCount(n => n + 1)}>{count}</button>;
}

The use API has distinct documented rules; do not generalize its exceptions to useState/useEffect.