Textarea - React
Textarea - React
v19
<textarea>
The built-in browser <textarea> component lets you render a
multiline text input.
<textarea />
Reference
<textarea>
Usage
Troubleshooting
Reference
<textarea>
https://react.dev/reference/react-dom/components/textarea 1/14
20/02/2025, 18:27 <textarea> – React
Props
When you pass value , you must also pass an onChange handler that
updates the passed value.
These <textarea> props are relevant both for uncontrolled and controlled
text areas:
children : <textarea> does not accept children. To set the initial value,
use defaultValue .
cols : A number. Specifies the default width in average character widths.
Defaults to 20 .
disabled : A boolean. If true , the input will not be interactive and will
appear dimmed.
form : A string. Specifies the id of the <form> this input belongs to. If
omitted, it’s the closest parent form.
maxLength : A number. Specifies the maximum length of text.
https://react.dev/reference/react-dom/components/textarea 2/14
20/02/2025, 18:27 <textarea> – React
name : A string. Specifies the name for this input that’s submitted with the
form.
onChange : An Event handler 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 browser input
event.
onChangeCapture : A version of onChange that fires in the capture phase.
required : A boolean. If true , the value must be provided for the form to
submit.
rows : A number. Specifies the default height in average character heights.
Defaults to 2 .
wrap : Either 'hard' , 'soft' , or 'off' . Specifies how the text should be
wrapped when submitting a form.
Caveats
Usage
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.
https://react.dev/reference/react-dom/components/textarea 4/14
20/02/2025, 18:27 <textarea> – React
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 .
Show more
https://react.dev/reference/react-dom/components/textarea 5/14
20/02/2025, 18:27 <textarea> – React
You can optionally specify the initial value for the text area. Pass it as the
defaultValue string.
https://react.dev/reference/react-dom/components/textarea 6/14
20/02/2025, 18:27 <textarea> – React
Pitfall
https://react.dev/reference/react-dom/components/textarea 7/14
20/02/2025, 18:27 <textarea> – React
Show more
Note
Pitfall
By default, any <button> inside a <form> will submit it. This can be
surprising! If you have your own custom Button React component,
consider returning <button type="button"> instead of <button> .
https://react.dev/reference/react-dom/components/textarea 8/14
20/02/2025, 18:27 <textarea> – React
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:
function NewPost() {
const [postContent, setPostContent] = useState(''); // Declare a state v
// ...
return (
<textarea
value={postContent} // ...force the input's value to match the state
onChange={e => setPostContent(e.target.value)} // ... and update the
/>
);
}
{
"dependencies": {
"react": "latest",
"react-dom": "latest",
"react-scripts": "latest",
https://react.dev/reference/react-dom/components/textarea 9/14
20/02/2025, 18:27 <textarea> – React
"remarkable": "2.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
Pitfall
you pass a state variable as a value but forget to update that state
variable synchronously during the onChange event handler, React will
revert the text area after every keystroke back to the value that you
specified.
https://react.dev/reference/react-dom/components/textarea 10/14
20/02/2025, 18:27 <textarea> – React
Troubleshooting
If you render a text area with value but no onChange , you will see an error in
the console:
Console
As the error message suggests, if you only wanted to specify the initial value,
pass defaultValue instead:
If you want to control this text area with a state variable, specify an
onChange handler:
If you control a text area, you must update its state variable to the text area’s
value from the DOM during onChange .
function handleChange(e) {
// 🔴 Bug: updating an input to something other than e.target.value
setFirstName(e.target.value.toUpperCase());
}
function handleChange(e) {
// 🔴 Bug: updating an input asynchronously
setTimeout(() => {
setFirstName(e.target.value);
}, 100);
}
function handleChange(e) {
// ✅ Updating a controlled input to e.target.value synchronously
setFirstName(e.target.value);
}
https://react.dev/reference/react-dom/components/textarea 12/14
20/02/2025, 18:27 <textarea> – React
If this doesn’t fix the problem, it’s possible that the text area gets removed
and re-added from the DOM on every keystroke. This can happen if you’re
accidentally resetting state on every re-render. For example, this can happen
if the text area or one of its parents always receives a different key attribute,
or if you nest component definitions (which is not allowed in React and
causes the “inner” component to remount on every render).
PREVIOUS
<select>
NEXT
<link>
https://react.dev/reference/react-dom/components/textarea 13/14
20/02/2025, 18:27 <textarea> – React
uwu?
Describing the UI
Adding Interactivity
Managing State
Escape Hatches
Community More
Acknowledgements Terms
https://react.dev/reference/react-dom/components/textarea 14/14