Multi-Select Dropdowns
What you'll learn: How to control a <select multiple> element by binding its value to an array in state and updating that array when selections change.
The Core Idea
You already know how to control a single-select dropdown with value={state} and onChange. Multi-select dropdowns work the same way, except the value prop expects an array of selected option values instead of a single string.
When a user selects or deselects options, you need to update state with a new array containing all currently selected values.
Basic Pattern
const [selectedFruits, setSelectedFruits] = useState([]);
<select
multiple
value={selectedFruits}
onChange={(e) => {
const options = Array.from(e.target.selectedOptions);
const values = options.map(opt => opt.value);
setSelectedFruits(values);
}}
>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="cherry">Cherry</option>
</select>
Breaking Down the Handler
e.target.selectedOptions: A collection of all currently selected<option>elements (not a real array).Array.from(...): Converts that collection into a real array..map(opt => opt.value): Extracts thevaluestring from each selected option.setSelectedFruits(values): Updates state with the new array.
The value={selectedFruits} prop tells React which options should appear selected based on your state array. If selectedFruits is ["apple", "cherry"], those two options will be highlighted.
Think of It Like a Checklist
Unlike a single dropdown (where you track one choice), a multi-select is like tracking multiple checkboxes. Your state is an array of "checked" items, and the dropdown reflects that list visually.
Key Takeaway: Control a <select multiple> by setting value to an array state variable and updating it with an array of selected option values extracted from e.target.selectedOptions.