Skip to article content
FrontendXLesson 04 / 40

Events, Controlled Inputs and Form Actions

Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.

beginner · about 55 minutes · 4 exercises

  • Predict
  • Implement
  • Debug
  • Interview

What you will be able to do

#
  • Handle synthetic events and read the event object.
  • Build controlled inputs and explain the value/onChange contract.
  • Use React 19 form actions with useActionState.

Overview

#

Synthetic events

React wraps native events in a synthetic event with a stable, cross-browser interface. Handlers receive the event as the first argument. In React 19 the event object is not pooled, so you can read it asynchronously.

snippet.tsx
function Search() {
  function onChange(e: React.ChangeEvent<HTMLInputElement>) {
    console.log(e.target.value);
  }
  return <input onChange={onChange} />;
}

Pass the function, do not call it: onClick={handle} not onClick={handle()}. To pass arguments, wrap in an arrow: onClick={() => remove(id)}.

Controlled inputs

A controlled input derives its value from state and reports changes through onChange. This makes React the single source of truth, which is required for validation, formatting and conditional enabling.

snippet.tsx
function NameField() {
  const [name, setName] = useState("");
  return (
    <input
      value={name}
      onChange={(e) => setName(e.target.value)}
    />
  );
}

If you set value without onChange, the input becomes read-only and React logs a warning. If you want the DOM to own the value, use defaultValue instead, which makes it uncontrolled.

Form actions in React 19

React 19 lets a form accept a function as its action. The function receives the submitted FormData. Combined with useActionState, you get pending state and a returned result without manual event wiring.

snippet.tsx
import { useActionState } from "react";

async function submit(_prev: string | null, formData: FormData) {
  const email = String(formData.get("email") ?? "");
  if (!email.includes("@")) return "Enter a valid email";
  return null;
}

function Signup() {
  const [error, formAction, isPending] = useActionState(submit, null);
  return (
    <form action={formAction}>
      <input name="email" type="email" />
      <button disabled={isPending}>{isPending ? "Sending" : "Sign up"}</button>
      {error && <p role="alert">{error}</p>}
    </form>
  );
}

useActionState returns the current result, a wrapped action and a pending flag. The form resets automatically after a successful action when uncontrolled inputs are used.

Accessibility basics

Every input needs an associated label. Use htmlFor on the label and id on the input, or wrap the input inside the label. Announce errors with role="alert" or an aria-live region so screen readers hear them.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    What happens when this input is typed into?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.tsx
    function Field() {
      const [v, setV] = useState("");
      return <input value={v} />;
    }
    Choose the best answer
  2. Exercise 2Implement

    Implement a controlled email field with a label and a submit button that is disabled until the value contains an at sign.

    starter.tsx
    export function EmailForm() {
      return null;
    }

    Reveal the reference answer
    reference.tsx
    export function EmailForm() {
      const [email, setEmail] = useState("");
      const valid = email.includes("@");
      return (
        <form>
          <label htmlFor="email">Email</label>
          <input
            id="email"
            value={email}
            onChange={(e) => setEmail(e.target.value)}
          />
          <button disabled={!valid}>Submit</button>
        </form>
      );
    }

    Authored expected output: The button is disabled until the field contains an at sign; the label is associated with the input.

    Derive validity during render from the controlled value instead of storing a separate flag. Associate the label with htmlFor and id for accessibility.

  3. Exercise 3Debug

    This button triggers an alert on page load. Fix the handler wiring.

    starter.tsx
    <button onClick={alert("hi")}>Click</button>

    Reveal the reference answer
    reference.tsx
    <button onClick={() => alert("hi")}>Click</button>

    Authored expected output: The alert appears only when the button is clicked.

    onClick={alert("hi")} calls alert during render and passes its return value (undefined) as the handler. Wrapping in an arrow defers the call to click time.

  4. Exercise 4Interview

    Explain the difference between controlled and uncontrolled inputs and when you would choose each.

    starter.tsx
    // A multi-step signup form with live validation.

    Reveal the reference answer
    reference.tsx
    Controlled inputs keep the value in React state, so the component can validate, format, disable buttons and coordinate fields on every keystroke. Uncontrolled inputs keep the value in the DOM and are read on submit, which is simpler and faster for large forms with few cross-field rules. For a multi-step form with live validation, controlled inputs are the right default because validation depends on current values. Use uncontrolled inputs with form actions when you only need the submitted data.

    Authored expected output: A comparison of source of truth, plus a justified choice for the described form.

    Interviewers look for the trade-off, not a rule. Controlled means React owns the value; uncontrolled means the DOM owns it. The choice follows from whether you need to react to changes before submit.

Common misconceptions

#
  • You should call the handler in JSX. Pass the reference; calling it runs during render.
  • value without onChange is a controlled input. It is read-only and React warns.
  • Controlled and uncontrolled inputs can be mixed freely. Switching between them triggers warnings and bugs.
  • Form actions are only for server components. Client components can use the action prop with useActionState.

Follow-up practice

#
  • react-effects-and-lifecycle
  • react-context-and-state-architecture

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.