Control text and selection inputs
Control a text field with state
Forms let users enter information and select options. A controlled field displays a value supplied by React state. Its change handler stores the new input so the next render displays it. Each independent App.jsx example in this lesson uses the Chapter 3 client app with an approved JSX toolchain. All visible outcomes are predictions pending browser tests.
import { useState } from 'react';export default function App() { const [title, setTitle] = useState(''); return <main><h1>Task title</h1> <label htmlFor="title">Title</label> <input id="title" name="title" value={title} onChange={event => setTitle(event.target.value)} /> <p>Preview: {title || '(empty)'}</p> <button type="button" onClick={() => setTitle('')}>Clear</button> </main>;}Type Water plants and the preview shows that title; Clear restores the empty input. The value is a string even when the field is empty. A controlled text field needs an onChange handler that promptly updates its backing value, unless intentionally read-only. Do not initialize a controlled string with undefined and later supply a string; choose an empty string instead.
Checkboxes, radio buttons, selects and textareas
import { useState } from 'react';export default function App() { const [urgent, setUrgent] = useState(false); const [period, setPeriod] = useState('morning'); const [area, setArea] = useState('home'); const [notes, setNotes] = useState(''); return <main><h1>Task preferences</h1> <label><input type="checkbox" name="urgent" checked={urgent} onChange={e => setUrgent(e.target.checked)} /> Urgent</label> <fieldset><legend>Time of day</legend> {['morning', 'evening'].map(time => <label key={time}> <input type="radio" name="period" value={time} checked={period === time} onChange={e => setPeriod(e.target.value)} /> {time} </label>)} </fieldset> <label>Area <select name="area" value={area} onChange={e => setArea(e.target.value)}> <option value="home">Home</option><option value="study">Study</option> </select></label> <label>Notes <textarea name="notes" value={notes} onChange={e => setNotes(e.target.value)} /></label> <p>{urgent ? 'Urgent' : 'Normal'} / {period} / {area} / {notes || 'No notes'}</p> </main>;}The checkbox reads a boolean from checked, while a radio option has a string value and a boolean checked state. The shared radio name groups the options. Select and textarea use string state here. A multiple select instead uses an array of selected option values, collected from selectedOptions. The summary begins Normal / morning / home / No notes and reflects each change.
Let the browser own a field when live state is unnecessary
import { useState } from 'react';export default function App() { const [summary, setSummary] = useState('Not saved'); function save(event) { event.preventDefault(); const data = new FormData(event.currentTarget); setSummary(`${data.get('title')} / ${data.has('done') ? 'Done' : 'Open'}`); } return <main><h1>Quick note</h1><form onSubmit={save}> <label>Title <input name="title" defaultValue="Read" /></label> <label><input type="checkbox" name="done" defaultChecked={false} /> Done</label> <button type="submit">Save</button> </form><p role="status">{summary}</p></main>;}The browser starts with Read and an unchecked box. Changes stay in the DOM until Save reads the form. FormData uses field names, not IDs, and an unchecked checkbox is absent. defaultValue and defaultChecked specify initial values rather than continuous control. A file input also leaves file selection to the browser; inspect its files property after selection instead of assigning a file path. Do not switch a mounted field between controlled and uncontrolled modes.
Practice
Add a controlled category select and a checkbox to the title preview. Check that clearing the title does not clear unrelated preferences.