学习库ReactForms in React

Forms in React

Build accessible controlled forms, submit real form data, and present validation clearly.

更新于 适用于 React 19.2

React works with native forms rather than replacing them. Keep labels, input types, keyboard submission, and built-in constraints.

A controlled field

SearchForm.tsx
function SearchForm() {
  const [query, setQuery] = useState("");
  return <form onSubmit={(event) => { event.preventDefault(); runSearch(query); }}>
    <label htmlFor="query">Search lessons</label>
    <input id="query" name="query" value={query} onChange={(event) => setQuery(event.target.value)} />
    <button type="submit">Search</button>
  </form>;
}

Controlled inputs take their current value from state. Uncontrolled inputs with defaultValue and FormData can be simpler when live state is unnecessary.

Validation feedback

Use native constraints first, validate again in the submit action or server, and connect custom error text with aria-describedby and aria-invalid.

Keep this

Preserve native form behavior, choose controlled state only when useful, and make errors specific, connected, and recoverable.

学习位置已保存在此设备上。