React Reference ยท Built-in DOM Components
The React textarea element
Reference for the React textarea element: control it with value and onChange, leave it uncontrolled with defaultValue, and avoid the children trap from HTML.
In plain HTML the starting text of a <textarea> is its children: <textarea>Hello</textarea>. React changes that. A textarea behaves like an <input>: you give it a value (controlled) or a defaultValue (uncontrolled), and you leave the children empty. This makes a textarea work with the same state, event and form patterns as every other field.
The onChange event is also different from the browser's. React's onChange fires on every edit, like the native input event, not only when the field loses focus. The new text is in event.target.value.
const [bio, setBio] = useState(''); <textarea value={bio} onChange={(e) => setBio(e.target.value)} rows={4} />
Try it
Syntax
<textarea value={string} defaultValue={string} onChange={(event) => {}} name="comment" rows={4} cols={40} />
| Name | Description |
|---|---|
value |
The text for a controlled textarea. Always pair it with onChange, or add readOnly. |
defaultValue |
The initial text for an uncontrolled textarea. Changing it later does not change what is shown. |
onChange |
Called on every edit with a synthetic event. Read the new text from event.target.value. |
rows, cols |
The initial size in lines and average character widths. CSS usually does this better. |
name, required, disabled, readOnly, placeholder, maxLength, minLength, autoComplete, form |
Same meaning as in HTML (camelCase where the DOM property is camelCase). |
children |
Do not use. React warns and treats them as a default value. Use defaultValue. |
Returns: a <textarea> element. Its DOM interface is HTMLTextAreaElement.
Edge cases: a controlled textarea with value but no onChange (and no readOnly) is frozen and logs a warning. Passing value={null} logs a warning (use an empty string), and going from value={undefined} to a string logs a controlled-versus-uncontrolled warning.
Usage notes
- Line breaks are
\n. The browser normalizes line breaks invalueto a single\n. When a form is submitted to a server, the browser converts them to\r\n. - Do not put a state value in the children.
<textarea>{text}</textarea>only sets the first value. Usevalue={text}. - Do not mix controlled and uncontrolled. Pass either
valueordefaultValue, never both, and do not switch fromundefinedto a string during the life of the field. Start state with'', notundefined. - Limiting length.
maxLengthstops typing and paste in the browser. Slicing inonChange(as above) also works, but the caret may jump to the end if you change the text while the user types in the middle. - Auto-growing fields. Set
rows={1}and, in an effect or handler, setstyle.heightto the element'sscrollHeight. Modern CSS can do this withfield-sizing: content. - Performance. A controlled textarea re-renders its component on every keystroke. For very large documents, keep the text in a ref or use an uncontrolled field and read it on submit.
- Form actions. Inside a
<form action={fn}>, an uncontrolled textarea is cleared automatically after a successful action. See form. - Accessibility. Give each textarea a label: wrap it in
<label>or connecthtmlForandid. A placeholder is not a label.
Related
<input>,<select>,<form>- Lessons: Forms and inputs, Uncontrolled and file forms