Handle a button action
#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.
Reference
Compare native component, state, event and lifecycle syntax in four frameworks.
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.
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.
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.
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.
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.
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.
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.