React Reference ยท Built-in DOM Components
The React select element
Reference for the React select element: set the chosen option with value, allow several with multiple and an array, and read the result from onChange.
A <select> lets the user pick one option (or several) from a list. In HTML you mark the chosen option with selected. React moves that decision to the <select> itself: you pass value (controlled) or defaultValue (uncontrolled) to the select, and React marks the matching <option> for you. This keeps the whole field in one place and makes it easy to drive from state.
With the multiple attribute the value is an array of strings. In both cases onChange fires when the choice changes and event.target.value holds the chosen option's value.
<select value={size} onChange={(e) => setSize(e.target.value)}> <option value="s">Small</option> <option value="m">Medium</option> </select>
Try it
Syntax
<select value={string | string[]} defaultValue={string | string[]} onChange={(event) => {}} multiple={boolean} > <option value="a">A</option> </select>
| Name | Description |
|---|---|
value |
The selected option value. A string, or an array of strings when multiple is set. Use with onChange. |
defaultValue |
The initially selected value for an uncontrolled select. |
onChange |
Called when the user changes the choice. |
multiple |
Allows several selections. Then value and defaultValue must be arrays. |
name, required, disabled, size, autoComplete, form |
Same as in HTML. size shows a list box with that many rows. |
children |
<option> and <optgroup> elements. |
Returns: a <select> element (HTMLSelectElement).
Edge cases: if multiple is set and value is not an array, React logs a warning. A value that matches no option leaves nothing selected in a multiple list, and the browser picks the first option in a single-choice dropdown (the displayed value then differs from your state, so keep state and options in sync).
Usage notes
- Never use
selectedon an option. React warns and tells you to usevalueordefaultValueon the<select>instead. See option. - Values are strings.
event.target.valueis always a string. Convert withNumber(...)if your option values are numbers, and compare with the same type you store. - Reading several choices. Use
Array.from(event.target.selectedOptions, (o) => o.value).event.target.valueonly gives the first selected option. - A placeholder option. Add
<option value="" disabled>Choose...</option>and start state with''so the field shows the prompt. - Required fields.
requiredfails validation while the selected option's value is an empty string. - Do not mix controlled and uncontrolled. Pass only one of
valueanddefaultValue. - Reset after a form action. An uncontrolled select in a
<form action={fn}>returns to its default selection after the action succeeds. See form. - Accessibility. Native selects are well supported by assistive technology, so prefer them to a custom dropdown unless you need rich content in the options. Label them with
<label>.
Related
<option>,<input>,<textarea>,<form>- Lessons: Forms and inputs, Form actions