An error message isn't useful just because it's red. Someone using a keyboard
or a screen reader needs three things after a failed submission:
The draft kept. Retyping everything is a punishment.
Each message tied to its field, so moving to the field announces the
problem: aria-invalid marks the field, and aria-describedby points at the
message's id.
Focus on the first problem, so they don't have to hunt for it.
Read, predict, run
DraftForm.jsx already does the first two. The Server Action saveDraft
returns the submitted values with the errors, and the inputs use them:
Run the lab, leave Title empty, type abc for Minutes, and press
Save draft with the mouse. Predict which element has keyboard focus
afterwards. (Press Tab once to find out: focus moves to the next element after
the focused one.)
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Check your prediction
Nothing in the form. The button had focus when you pressed it, but it's
disabled while the action is pending, and a disabled element can't keep
focus, so the browser drops it to the page itself. Tab starts again from the
top of the page. Both messages are on screen and the draft is kept, but a
screen reader user hears none of it unless they go looking.
The failure to avoid
Moving focus is a side effect, so it doesn't belong in render: calling
focus() while rendering runs before the inputs are on the page, and runs again
on every render. Tie it to a failed submission, not to typing: focusing on
every keystroke would yank the cursor around.
Exercise
Focus the first problem
After a failed submission, move focus to the first invalid field: Title if
it's empty, otherwise Minutes. A successful save must not move focus.
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
useActionState gives you a new state object after every submission, and
only then. An effect that depends on state therefore runs once per
submission, after React has put the inputs on the page, which is exactly when
focus should move.
Hint 2: a first step
Add a useEffect with [state] as its dependency that logs run(state.errors).
Submit a few times: it should log "title", "minutes" or null. The refs in
fields are already attached to the inputs, so fields.title.current is the
title input.
Show one solution
JSX
useEffect(() => { const first = run(state.errors); if (first) fields[first].current.focus();}, [state]);
Add it after the fields line, and import useEffect from react. run
returns a field name, which is also a key of fields, so one line picks the
right input. On success errors is empty, run returns null, and focus is
left alone. Depending on [state.errors] instead would also work here, since a
new object arrives each time; depending on nothing would focus only once, on
the first render.
Explain it without the code
A form has five fields and the third and fifth are invalid. Write what a
keyboard user experiences after pressing Save, with and without this
effect, in two sentences.
Going further
Some forms also put a summary of all errors at the top in a role="alert"
region, with links to each field. That helps long forms; for two fields, focus
on the first problem is enough. React's
useRef guide covers
focusing elements.
Completion is saved in this browser only. It does not measure mastery.