Select and Textarea in React
Learn how to handle dropdown lists and multiline text inputs in React using state and event handlers.
1. Introduction to Select and Textarea
A select element allows users to choose an option from a dropdown list. A textarea allows users to enter multiple lines of text, such as feedback, comments, or a course enquiry.
In React, both elements can be controlled using state. The value prop stores the current selection or text, while onChange updates the state when the user changes the input.
value and onChange to manage
controlled select and textarea elements in React.
2. Handling a Select Element
A select element displays a dropdown list. React state can store the selected option and update it whenever the user chooses another option.
Example
import { useState } from "react";
function CourseSelect() {
const [course, setCourse] = useState("React");
function handleChange(event) {
setCourse(event.target.value);
}
return (
<div>
<label htmlFor="course">Choose a Course:</label>
<select
id="course"
value={course}
onChange={handleChange}
>
<option value="React">React</option>
<option value="Angular">Angular</option>
<option value="Python">Python</option>
<option value="Data Analytics">
Data Analytics
</option>
</select>
<p>Selected Course: {course}</p>
</div>
);
}
export default CourseSelect;
Expected Output
Selected Course: React
The course state initially contains React. When the user selects another option, handleChange reads the selected value and updates the state.
3. Handling a Textarea Element
A textarea is useful when users need to enter longer text. For example, students may provide feedback or describe their learning requirements.
Example
import { useState } from "react";
function FeedbackInput() {
const [feedback, setFeedback] = useState("");
function handleChange(event) {
setFeedback(event.target.value);
}
return (
<div>
<label htmlFor="feedback">Your Feedback:</label>
<textarea
id="feedback"
value={feedback}
onChange={handleChange}
rows={4}
/>
<p>Feedback: {feedback}</p>
</div>
);
}
export default FeedbackInput;
Expected Output
Feedback: Great learning experience at CIIT Training Institute!
The feedback state stores the text entered by the user. Every change updates the state, and React displays the current feedback below the textarea.
4. Setting a Default Selection
A controlled select element can have an initial selection by setting the initial state value. The select value should match one of the option values.
Example
import { useState } from "react";
function PreferredCourse() {
const [course, setCourse] = useState("Data Analytics");
return (
<div>
<label htmlFor="preferredCourse">
Preferred Course:
</label>
<select
id="preferredCourse"
value={course}
onChange={(event) =>
setCourse(event.target.value)
}
>
<option value="React">React</option>
<option value="Angular">Angular</option>
<option value="Data Analytics">
Data Analytics
</option>
</select>
<p>Preferred Course: {course}</p>
</div>
);
}
export default PreferredCourse;
Expected Output
Preferred Course: Data Analytics
Because the initial state is Data Analytics, that option appears selected when the component first renders.
5. Handling Multiple Lines of Text
The rows attribute controls the visible height of a textarea. You can use it to provide enough space for users to write comments, questions, or course enquiries.
Example
<textarea
rows={5}
value={message}
onChange={(event) =>
setMessage(event.target.value)
}
placeholder="Enter your enquiry"
/>
Expected Output
The textarea provides multiple visible lines for entering text. The value and onChange props connect it to React state.
6. CIIT Training Institute Course Enquiry Form
Consider Samadhan Samiksha submitting a course enquiry to CIIT Training Institute. This example combines a select element and a textarea in one controlled form.
Example
import { useState } from "react";
function CourseEnquiry() {
const [studentName, setStudentName] =
useState("Samadhan");
const [course, setCourse] = useState("React");
const [message, setMessage] = useState("");
function handleSubmit(event) {
event.preventDefault();
console.log({
studentName,
course,
message
});
}
return (
<form onSubmit={handleSubmit}>
<h2>CIIT Training Institute</h2>
<label htmlFor="studentName">
Student Name:
</label>
<input
id="studentName"
value={studentName}
onChange={(event) =>
setStudentName(event.target.value)
}
/>
<label htmlFor="course">
Select Course:
</label>
<select
id="course"
value={course}
onChange={(event) =>
setCourse(event.target.value)
}
>
<option value="React">React</option>
<option value="Angular">Angular</option>
<option value="Data Analytics">
Data Analytics
</option>
</select>
<label htmlFor="message">
Your Enquiry:
</label>
<textarea
id="message"
value={message}
onChange={(event) =>
setMessage(event.target.value)
}
rows={4}
/>
<button type="submit">
Submit Enquiry
</button>
</form>
);
}
export default CourseEnquiry;
Expected Console Output
{
studentName: "Samadhan",
course: "React",
message: "Please share the course details."
}
When the form is submitted, the handler prevents the browser's default form submission and logs the current values. This example demonstrates form handling; it does not save the enquiry to a database or send it to the institute.
7. Handling Changes with onChange
The onChange event handler receives an event object. For a select element, event.target.value contains the selected option. For a textarea, it contains the current text.
Select Handler
function handleCourseChange(event) {
setCourse(event.target.value);
}
Textarea Handler
function handleMessageChange(event) {
setMessage(event.target.value);
}
8. Common Mistakes
Mistake 1: Using value Without onChange
<select value={course}>
<option value="React">React</option>
<option value="Angular">Angular</option>
</select>
The select is controlled by course, but no handler updates the state when the selection changes.
Correct Code
<select
value={course}
onChange={(event) =>
setCourse(event.target.value)
}
>
<option value="React">React</option>
<option value="Angular">Angular</option>
</select>
Mistake 2: Not Matching the Option Value
const [course, setCourse] = useState("Python");
return (
<select value={course} onChange={handleChange}>
<option value="React">React</option>
<option value="Angular">Angular</option>
</select>
);
The state contains Python, but there is no option with that value. The initial state should match an available option, or the option list should include Python.
Mistake 3: Using Children Instead of State for a Controlled Textarea
<textarea value={message}>
Initial message
</textarea>
For a controlled textarea, provide the content through value rather than placing initial text between its tags.
Correct Code
<textarea
value={message}
onChange={(event) =>
setMessage(event.target.value)
}
/>
9. Practice Tasks
Complete these tasks to practise select and textarea elements.
Summary
Select elements allow users to choose options from a dropdown, while textarea elements collect multiline text. In React, both can be managed as controlled components using value and onChange. State stores the selected option or entered text, and event.target.value provides the current value when a change occurs. These controls can be combined to build course enquiry forms that collect a student's preferred course and additional questions for CIIT Training Institute.