Course contentsShow
← React
Lesson 486 of 2,39613. Controlled ComponentsFree lesson

Multi-Select: value as Array

Handle multiple={true} selects with array state and Array.from(e.target.selectedOptions).

Multi-Select: value as Array

What you'll learn: How to control a multi-select dropdown by storing an array of selected values in state and syncing it with user selections.

The Core Idea

With a regular <select>, the value prop is a single string. But when you add multiple={true}, users can select multiple options at once. To control this, your state must be an array of strings, and your onChange handler needs to extract all selected options—not just one.

How It Works

Set your <select> element's value prop to an array stored in state:

const [selected, setSelected] = useState([]);

<select multiple={true} value={selected} onChange={handleChange}>
  <option value="apple">Apple</option>
  <option value="banana">Banana</option>
  <option value="cherry">Cherry</option>
</select>

When the user selects or deselects options, event.target.selectedOptions gives you an HTMLCollection (not an array). Convert it to an array and extract the values:

function handleChange(event) {
  const options = Array.from(event.target.selectedOptions);
  const values = options.map(opt => opt.value);
  setSelected(values);
}

Now selected might be ["apple", "cherry"], and React will highlight those two options in the dropdown.

Why This Pattern

Think of the state array as a checklist. Each item in the array tells React "this option is selected." Without an array, React can't track multiple selections. Array.from() transforms the browser's collection into something you can map over to pull out the values.

Key Takeaway: Control a multi-select by setting value to an array state and using Array.from(event.target.selectedOptions).map(opt => opt.value) in your onChange handler to capture all selected values.