Handle change and submit events
Read a changed field
React passes an event object to a handler. In a text input onChange handler, read the current input value and update the displayed state. For a checkbox, read checked instead. React DOM onChange for a text field responds to user edits as they happen, unlike waiting for the native change event on a text field to commit.
import { useState } from 'react';
export default function App() { const [query, setQuery] = useState(''); return <main><h1>Trail search</h1> <label>Trail name <input value={query} onChange={event => setQuery(event.currentTarget.value)} /></label> <p>Search draft: {query || '(empty)'}</p> </main>;}Typing Oak displays Search draft: Oak. Clearing the input displays Search draft: (empty). The state supplies value and onChange updates that state using the edited field. This is a controlled input; keep its value a string throughout its lifetime. The wrapping label names the field without relying on a placeholder.
Respond to submitting a form
import { useState } from 'react';
function cleanNote(text) { return text.trim();}
function NoteForm() { const [draft, setDraft] = useState(''); const [message, setMessage] = useState('No note saved.'); function handleSubmit(event) { event.preventDefault(); const note = cleanNote(draft); if (!note) { setMessage('Enter a nonblank note.'); return; } setMessage('Saved locally: ' + note); setDraft(''); } return <form onSubmit={handleSubmit}> <label>Trail note <input value={draft} onChange={e => setDraft(e.currentTarget.value)} /></label> <button type="submit">Save note</button> <p role="status">{message}</p> </form>;}
export default function App() { return <main><h1>Trail notes</h1><NoteForm /></main>;}A blank or spaces-only note produces a message without submitting to a server. Entering Dry path with surrounding spaces and submitting displays Saved locally: Dry path and clears the field. This is only in-memory UI state, not persistent storage. The form handles submission from the button or ordinary keyboard submission. preventDefault cancels the normal browser submission for this local demo; it does not stop other handlers. The input is intentionally not required so the custom blank-note path is observable. Client validation is not a security boundary.
Read a checkbox correctly
import { useState } from 'react';
export default function App() { const [accessible, setAccessible] = useState(false); return <main><h1>Trail preferences</h1> <label><input type="checkbox" checked={accessible} onChange={event => setAccessible(event.currentTarget.checked)} /> Show step-free trails only</label> <p>{accessible ? 'Step-free only' : 'All trails'}</p> </main>;}The checkbox starts unchecked and the text reads All trails. Checking it changes the text to Step-free only; unchecking restores All trails. Reading value instead of checked would not retrieve the selection boolean. For buttons inside forms that should not submit, set type="button". Full form validation, uncontrolled inputs and more field types come in Chapter 12.
Practice
Build a local equipment request form with a labeled text input and submit button. Trim the input, reject blank values, show the accepted value without navigating, and clear the draft only on success. Try both Enter and the submit button.