Build useActionState and form actions from scratch: a sequential queue, pending state, errors and form reset

hardAbout 55 minutesUpdated CodingProreactreact-19useactionstateuseformstatusformsactionsinternals
Not solved

Share this question

Problem

React 19 turned forms into a data flow: <form action={fn}> calls fn(formData) inside a transition, useActionState keeps the result of the last action, and useFormStatus lets a submit button inside the form know that it is pending. Interviewers now ask how this works underneath: what happens when the user submits twice, which state the second action sees, and where errors go. Rebuild the three pieces in user land.

const [state, dispatch, isPending] = useActionState(addTodo, { todos: [], error: null });

<Form action={dispatch}>
  <input name="title" />
  <SubmitButton /> {/* reads useFormStatus().pending */}
</Form>

Ref. UR-1434 · © UIRound · uiround.com/questions/build-useactionstate-and-form-actions-from-scratch-a-sequential-queue-pending-state-errors-and-form-reset

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment