<select>
The built-in browser <select> component lets you render a select box with options.
<select> <option value="someOption">Some option</option> <option value="otherOption">Other option</option> </select>
Reference
<select>
To display a select box, render the built-in browser <select> component.
<select> <option value="someOption">Some option</option> <option value="otherOption">Other option</option> </select>
Props
<select> supports all common element props.
You can make a select box controlled by passing a value prop:
value: A string (or an array of strings formultiple={true}). Controls which option is selected. Every value string match thevalueof some<option>nested inside the<select>.
When you pass value, you must also pass an onChange handler that updates the passed value.
If your <select> is uncontrolled, you may pass the defaultValue prop instead:
defaultValue: A string (or an array of strings formultiple={true}). Specifies the initially selected option.
These <select> props are relevant both for uncontrolled and controlled select boxes:
autoComplete: A string. Specifies one of the possible autocomplete behaviors.autoFocus: A boolean. Iftrue, React will focus the element on mount.children:<select>accepts<option>,<optgroup>, and<datalist>components as children. You can also pass your own components as long as they eventually render one of the allowed components. If you pass your own components that eventually render<option>tags, each<option>you render must have avalue.disabled: A boolean. Iftrue, the select box will not be interactive and will appear dimmed.form: A string. Specifies theidof the<form>this select box belongs to. If omitted, it’s the closest parent form.multiple: A boolean. Iftrue, the browser allows multiple selection.name: A string. Specifies the name for this select box that’s submitted with the form.onChange: AnEventhandler function. Required for controlled select boxes. Fires immediately when the user picks a different option. Behaves like the browserinputevent.onChangeCapture: A version ofonChangethat fires in the capture phase.onInput: AnEventhandler function. Fires immediately when the value is changed by the user. For historical reasons, in React it is idiomatic to useonChangeinstead which works similarly.onInputCapture: A version ofonInputthat fires in the capture phase.onInvalid: AnEventhandler function. Fires if an input fails validation on form submit. Unlike the built-ininvalidevent, the ReactonInvalidevent bubbles.onInvalidCapture: A version ofonInvalidthat fires in the capture phase.required: A boolean. Iftrue, the value must be provided for the form to submit.size: A number. Formultiple={true}selects, specifies the preferred number of initially visible items.
Caveats
- Unlike in HTML, passing a
selectedattribute to<option>is not supported. Instead, use<select defaultValue>for uncontrolled select boxes and<select value>for controlled select boxes. - If a select box receives a
valueprop, it will be treated as controlled. - A select box can’t be both controlled and uncontrolled at the same time.
- A select box cannot switch between being controlled or uncontrolled over its lifetime.
- Every controlled select box needs an
onChangeevent handler that synchronously updates its backing value.
Usage
Displaying a select box with options
Render a <select> with a list of <option> components inside to display a select box. Give each <option> a value representing the data to be submitted with the form.
Providing a label for a select box
Typically, you will place every <select> inside a <label> tag. This tells the browser that this label is associated with that select box. When the user clicks the label, the browser will automatically focus the select box. It’s also essential for accessibility: a screen reader will announce the label caption when the user focuses the select box.
If you can’t nest <select> into a <label>, associate them by passing the same ID to <select id> and <label htmlFor>. To avoid conflicts between multiple instances of one component, generate such an ID with useId.
Providing an initially selected option
By default, the browser will select the first <option> in the list. To select a different option by default, pass that <option>’s value as the defaultValue to the <select> element.
Enabling multiple selection
Pass multiple={true} to the <select> to let the user select multiple options. In that case, if you also specify defaultValue to choose the initially selected options, it must be an array.
Reading the select box value when submitting a form
Add a <form> around your select box with a <button type="submit"> inside. It will call your <form onSubmit> event handler. By default, the browser will send the form data to the current URL and refresh the page. You can override that behavior by calling e.preventDefault(). Read the form data with new FormData(e.target).
Controlling a select box with a state variable
A select box like <select /> is uncontrolled. Even if you pass an initially selected value like <select defaultValue="orange" />, your JSX only specifies the initial value, not the value right now.
To render a controlled select box, pass the value prop to it. React will force the select box to always have the value you passed. Typically, you will control a select box by declaring a state variable:
function FruitPicker() { const [selectedFruit, setSelectedFruit] = useState('orange'); // Declare a state variable... // ... return ( <select value={selectedFruit} // ...force the select's value to match the state variable... onChange={e => setSelectedFruit(e.target.value)} // ... and update the state variable on any change! > <option value="apple">Apple</option> <option value="banana">Banana</option> <option value="orange">Orange</option> </select> ); }
This is useful if you want to re-render some part of the UI in response to every selection.