liveBook · Manning

Text box

As explained in React Quickly, Second Edition

A text field is a form control that accepts character input from a user.

A text box lets a user enter characters into a form. In React, a common approach is to keep its current value in component state and update that state when the user types. This is a controlled input; an uncontrolled input instead leaves its value under the browser’s control until React reads it. section 9.0

Add a description field

A multiline text box uses `<textarea>`. Give it a state-backed `value` and update that state through `onChange`: `const [data, setData] = useState({ title: "", description: "" });` `const onChange = (evt) => setData((old) => ({ ...old, [evt.target.name]: evt.target.value }));` `<textarea name="description" value={data.description} onChange={onChange} />` section 9.2.5

There is a small HTML-to-React difference to remember: plain HTML puts an input’s initial text in its `value` attribute but puts a textarea’s text between its tags. In React, use `value` for the textarea too. section 9.2.5

Try it in a task list

Place an input field at the end of each task’s ordered list of steps, inside a form with a button. Use it to add a new step; keep each existing step’s completion checkbox separate from the text entry field. section 13.3

How React Quickly, Second Edition covers it

How deep it goes
Works through a state-backed textarea and explains its HTML-to-React syntax difference, without detailing text-box validation.
Examples
Uses a to-do description textarea and an exercise that adds steps through an input and button.
What it assumes
Assumes familiarity with basic HTML forms and React state while explaining the textarea’s HTML-to-React difference.
What you can do after
Build it from scratch: add a state-backed description textarea or a new-step input to a React form.
How it connects
Connects text fields to forms, component state, change events, controlled inputs, uncontrolled inputs, and textareas.

Also explained in

Compare how 7 books explain text box →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Quickly, Second Edition. The section links point to the parts of the book it draws on.