intermediate · 22 minute study
A form is state, semantics and feedback
A useful form must remain coherent while typing, validating, submitting and handling failure—with a keyboard as well as a pointer.
Useful preparation · 1 prerequisite
Use this as a map, not a lock. If these ideas are familiar, continue.
- Choose one owner for each fact — Duplicated state makes forms, filters, selected rows and totals disagree with one another.
First, build a mental model
A controlled input displays the value owned by React state. The browser event reports a proposed change; your handler updates that state. Labels and feedback make the workflow understandable.
- Pair value with an onChange that updates the owning state.
- Keep editable text as text until an explicit validation/conversion boundary.
- Use a real form and submit button for expected browser/keyboard behavior.
- Associate errors and labels with inputs and preserve a clear focus path.
function QuantityForm() {
const [text, setText] = useState('1');
const quantity = Number(text);
const valid = text.trim() !== '' && Number.isInteger(quantity) && quantity > 0;
return <form onSubmit={event => { event.preventDefault(); if (valid) save(quantity); }}>
<label htmlFor='quantity'>Quantity</label>
<input id='quantity' value={text} onChange={event => setText(event.target.value)} inputMode='numeric' aria-describedby='quantity-help' />
<p id='quantity-help'>Enter a positive whole number.</p>
<button type='submit' disabled={!valid}>Save</button>
</form>;
}- Type
The input can temporarily hold empty or incomplete text.
- Validate
Only a nonempty positive whole number is acceptable.
- Submit
The workflow prevents navigation and commits only a validated value.
Production thinking
Take this beyond the example.
Validate at every untrusted boundary; prevent duplicate submits where appropriate and handle uncertain outcomes with request identity/idempotency rather than hopeful UI disabling.
Sources and further reading
Your evidence
unseen is a local learning signal, not a credential or proof of production competence.
Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.
Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.
Next lesson · Name events; centralize transitions →