How to create a React Select

 by Robin Wieruch
 - Edit this Post

A short React tutorial by example for beginners about creating a select in React. First of all, a select is just an HTML select element which can be rendered in React's JSX:

import * as React from 'react';
const App = () => {
return (
<div>
<select>
<option value="fruit">Fruit</option>
<option value="vegetable">Vegetable</option>
<option value="meat">Meat</option>
</select>
</div>
);
};
export default App;

What may be missing is an associated label to signal the user what value is changed with this select:

import * as React from 'react';
const App = () => {
return (
<div>
<label>
What do we eat?
<select>
<option value="fruit">Fruit</option>
<option value="vegetable">Vegetable</option>
<option value="meat">Meat</option>
</select>
</label>
</div>
);
};
export default App;

In your browser, this select can already change its select state by showing every of its values individually. However, this is just the select's internal HTML state which isn't controlled by React yet. Let's change this by transforming this select from being :

import * as React from 'react';
const App = () => {
const [value, setValue] = React.useState('fruit');
const handleChange = (event) => {
setValue(event.target.value);
};
return (
<div>
<label>
What do we eat?
<select value={value} onChange={handleChange}>
<option value="fruit">Fruit</option>
<option value="vegetable">Vegetable</option>
<option value="meat">Meat</option>
</select>
</label>
<p>We eat {value}!</p>
</div>
);
};
export default App;

By using and an , we can keep track of the select's value via . We can refine this component by rendering the options more dynamically:

import * as React from 'react';
const App = () => {
const options = [
{ label: 'Fruit', value: 'fruit' },
{ label: 'Vegetable', value: 'vegetable' },
{ label: 'Meat', value: 'meat' },
];
const [value, setValue] = React.useState('fruit');
const handleChange = (event) => {
setValue(event.target.value);
};
return (
<div>
<label>
What do we eat?
<select value={value} onChange={handleChange}>
{options.map((option) => (
<option value={option.value}>{option.label}</option>
))}
</select>
</label>
<p>We eat {value}!</p>
</div>
);
};
export default App;

A great analogy for this refactoring of the component is the list component in React](/react-list-component/). Next we may want to create a Select component which can be used more than once throughout a React project. Therefore, we will extract it as a new and to it:

import * as React from 'react';
const App = () => {
const options = [
{ label: 'Fruit', value: 'fruit' },
{ label: 'Vegetable', value: 'vegetable' },
{ label: 'Meat', value: 'meat' },
];
const [value, setValue] = React.useState('fruit');
const handleChange = (event) => {
setValue(event.target.value);
};
return (
<div>
<Select
label="What do we eat?"
options={options}
value={value}
onChange={handleChange}
/>
<p>We eat {value}!</p>
</div>
);
};
const Select = ({ label, value, options, onChange }) => {
return (
<label>
{label}
<select value={value} onChange={onChange}>
{options.map((option) => (
<option value={option.value}>{option.label}</option>
))}
</select>
</label>
);
};
export default App;

Our Select component is a now. For example, if you would give your select element some , every Select component which is used in your React project would use the same style.

If you would want to create a select group now, you could just use multiple Select components side by side and maybe style them with some border and some alignment, so that a user perceives them as a group of select components:

import * as React from 'react';
const App = () => {
const [food, setFood] = React.useState('fruit');
const [drink, setDrink] = React.useState('water');
const handleFoodChange = (event) => {
setFood(event.target.value);
};
const handleDrinkChange = (event) => {
setDrink(event.target.value);
};
return (
<div>
<Select
label="What do we eat?"
options={[
{ label: 'Fruit', value: 'fruit' },
{ label: 'Vegetable', value: 'vegetable' },
{ label: 'Meat', value: 'meat' },
]}
value={food}
onChange={handleFoodChange}
/>
<Select
label="What do we drink?"
options={[
{ label: 'Water', value: 'water' },
{ label: 'Beer', value: 'beer' },
{ label: 'Wine', value: 'wine' },
]}
value={drink}
onChange={handleDrinkChange}
/>
<p>We eat {food}!</p>
<p>We drink {drink}!</p>
</div>
);
};
export default App;

That's is it for creating a Select component in React. If you are a beginner in React, I hope this tutorial helped you to understand some concepts and patterns!

Keep reading about 

A short React tutorial by example for beginners about using a checkbox in React. First of all, a checkbox is just an HTML input field with the type of checkbox which can be rendered in React's JSX…

A short React tutorial by example for beginners about using a radio button in React. First of all, a radio button is just an HTML input field with the type of radio which can be rendered in React's…

The Road to React

Learn React by building real world applications. No setup configuration. No tooling. Plain React in 200+ pages of learning material. Learn React like 50.000+ readers.

Get it on Amazon.