<textarea>
The built-in browser <textarea> component lets you render a multiline text input.
<textarea />
Reference
<textarea>
To display a text area, render the built-in browser <textarea> component.
<textarea name="postContent" />
Props
<textarea> supports all common element props.
You can make a text area controlled by passing a value prop:
value: A string. Controls the text inside the text area.
When you pass value, you must also pass an onChange handler that updates the passed value.
If your <textarea> is uncontrolled, you may pass the defaultValue prop instead:
defaultValue: A string. Specifies the initial value for a text area.
These <textarea> props are relevant both for uncontrolled and controlled text areas:
autoComplete: Either'on'or'off'. Specifies the autocomplete behavior.autoFocus: A boolean. Iftrue, React will focus the element on mount.children:<textarea>does not accept children. To set the initial value, usedefaultValue.cols: A number. Specifies the default width in average character widths. Defaults to20.disabled: A boolean. Iftrue, the input will not be interactive and will appear dimmed.form: A string. Specifies theidof the<form>this input belongs to. If omitted, it's the closest parent form.maxLength: A number. Specifies the maximum length of text.minLength: A number. Specifies the minimum length of text.name: A string. Specifies the name for this input that's submitted with the form.onChange: AnEventhandler function. Required for controlled text areas. Fires immediately when the input's value is changed by the user (for example, it fires on every keystroke). Behaves like the browserinputevent.onChangeCapture: A version ofonChangethat fires in the capture phase.onInput: AnEventhandler function. Fires immediately when the value is changed by the user. For historical reasons, in React it is idiomatic to useonChangeinstead which works similarly.onInputCapture: A version ofonInputthat fires in the capture phase.onInvalid: AnEventhandler function. Fires if an input fails validation on form submit. Unlike the built-ininvalidevent, the ReactonInvalidevent bubbles.onInvalidCapture: A version ofonInvalidthat fires in the capture phase.onSelect: AnEventhandler function. Fires after the selection inside the<textarea>changes. React extends theonSelectevent to also fire for empty selection and on edits (which may affect the selection).onSelectCapture: A version ofonSelectthat fires in the capture phase.placeholder: A string. Displayed in a dimmed color when the text area value is empty.readOnly: A boolean. Iftrue, the text area is not editable by the user.required: A boolean. Iftrue, the value must be provided for the form to submit.rows: A number. Specifies the default height in average character heights. Defaults to2.wrap: Either'hard','soft', or'off'. Specifies how the text should be wrapped when submitting a form.
Caveats
- Passing children like
<textarea>something</textarea>is not allowed. UsedefaultValuefor initial content. - If a text area receives a string
valueprop, it will be treated as controlled. - A text area can't be both controlled and uncontrolled at the same time.
- A text area cannot switch between being controlled or uncontrolled over its lifetime.
- Every controlled text area needs an
onChangeevent handler that synchronously updates its backing value.
Usage
Displaying a text area
Render <textarea> to display a text area. You can specify its default size with the rows and cols attributes, but by default the user will be able to resize it. To disable resizing, you can specify resize: none in the CSS.
export default function NewPost() {
return (
<label>
Write your post:
<textarea name="postContent" rows={4} cols={40} />
</label>
);
}
input { margin-left: 5px; }
textarea { margin-top: 10px; }
label { margin: 10px; }
label, textarea { display: block; }
Providing a label for a text area
Typically, you will place every <textarea> inside a <label> tag. This tells the browser that this label is associated with that text area. When the user clicks the label, the browser will focus the text area. It's also essential for accessibility: a screen reader will announce the label caption when the user focuses the text area.
If you can't nest <textarea> into a <label>, associate them by passing the same ID to <textarea id> and <label htmlFor>. To avoid conflicts between instances of one component, generate such an ID with useId.
import { useId } from 'react';
export default function Form() {
const postTextAreaId = useId();
return (
<>
<label htmlFor={postTextAreaId}>
Write your post:
</label>
<textarea
id={postTextAreaId}
name="postContent"
rows={4}
cols={40}
/>
</>
);
}
input { margin: 5px; }
Providing an initial value for a text area
You can optionally specify the initial value for the text area. Pass it as the defaultValue string.
export default function EditPost() {
return (
<label>
Edit your post:
<textarea
name="postContent"
defaultValue="I really enjoyed biking yesterday!"
rows={4}
cols={40}
/>
</label>
);
}
input { margin-left: 5px; }
textarea { margin-top: 10px; }
label { margin: 10px; }
label, textarea { display: block; }
[!WARNING]
Unlike in HTML, passing initial text like
<textarea>Some content</textarea>is not supported.
Reading the text area value when submitting a form
Add a <form> around your textarea with a <button type="submit"> inside. It will call your <form onSubmit> event handler. By default, the browser will send the form data to the current URL and refresh the page. You can override that behavior by calling e.preventDefault(). Read the form data with new FormData(e.target).
export default function EditPost() {
function handleSubmit(e) {
// Prevent the browser from reloading the page
e.preventDefault();
// Read the form data
const form = e.target;
const formData = new FormData(form);
// You can pass formData as a fetch body directly:
fetch('/some-api', { method: form.method, body: formData });
// Or you can work with it as a plain object:
const formJson = Object.fromEntries(formData.entries());
console.log(formJson);
}
return (
<form method="post" onSubmit={handleSubmit}>
<label>
Post title: <input name="postTitle" defaultValue="Biking" />
</label>
<label>
Edit your post:
<textarea
name="postContent"
defaultValue="I really enjoyed biking yesterday!"
rows={4}
cols={40}
/>
</label>
<hr />
<button type="reset">Reset edits</button>
<button type="submit">Save post</button>
</form>
);
}
label { display: block; }
input { margin: 5px; }
[!NOTE]
Give a
nameto your<textarea>, for example<textarea name="postContent" />. Thenameyou specified will be used as a key in the form data, for example{ postContent: "Your post" }.[!WARNING]
By default, any
<button>inside a<form>will submit it. This can be surprising! If you have your own customButtonReact component, consider returning<button type="button">instead of<button>. Then, to be explicit, use<button type="submit">for buttons that are supposed to submit the form.
Controlling a text area with a state variable
A text area like <textarea /> is uncontrolled. Even if you pass an initial value like <textarea defaultValue="Initial text" />, your JSX only specifies the initial value, not the value right now.
To render a controlled text area, pass the value prop to it. React will force the text area to always have the value you passed. Typically, you will control a text area by declaring a state variable:
```js {2,6,7} function NewPost() { const [postContent, setPostContent] = useState(''); // Declare a state variable... // ... return (
