React Foundations · Actions & Forms

Handling a form with a function action

Form actions

Handling a form with a function action

A form gathers several fields and sends them together. The usual way wires an onChange to every input and an onSubmit to the form, tracking each field by hand. React 19 lets you hand the whole form to one function instead: a form action.

Pass a function to the form's action prop:

<form action={(formData) => save(formData)}>
  <input name="name" />
  <button type="submit">Save</button>
</form>

On submit, React stops the browser's page reload and calls your function with a FormData built from the form's fields. You read a field by its name:

const name = String(formData.get('name'));

Because React reads the inputs at submit time, you do not need an onChange on every field. The action can update state — or call a Server Function in a server app — and React re-renders when it returns. The <form> keeps working without JavaScript too, because the action lives on the form itself.

Your job is to read a submitted field in a form action in App.jsx and render the result.

Challenge

A form collects a name but never reads it back. Export an App component from App.jsx. Inside it, render a <form> with a text input named name, a submit button labelled Save, and a paragraph. Give the form a function action that reads the name field from its FormData into state, then render the paragraph as Hello <name>.

The Test types Ada, submits the form, and expects the paragraph to read Hello Ada. The starter never reads the form, so the Test fails.

App.jsx
export function App() {
  return (
    <form aria-label="Greeting">
      <input name="name" />
      <button type="submit">Save</button>
      <p>Hello</p>
    </form>
  );
}

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.