How to create a React Dropdown

 by Robin Wieruch
 - Edit this Post

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

const App = () => {
return (
<div>
<select>
<option value="fruit">Fruit</option>
<option value="vegetable">Vegetable</option>
<option value="meat">Meat</option>
</select>
</div>
);
};

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

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>
);
};

In your browser, this dropdown 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 :

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>
);
};

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:

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>
);
};

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 Dropdown component which can be used more than once throughout a React project. Therefore, we will extract it as a new and to it:

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>
<Dropdown
label="What do we eat?"
options={options}
value={value}
onChange={handleChange}
/>
<p>We eat {value}!</p>
</div>
);
};
const Dropdown = ({ label, value, options, onChange }) => {
return (
<label>
{label}
<select value={value} onChange={onChange}>
{options.map((option) => (
<option value={option.value}>{option.label}</option>
))}
</select>
</label>
);
};

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

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

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>
<Dropdown
label="What do we eat?"
options={[
{ label: 'Fruit', value: 'fruit' },
{ label: 'Vegetable', value: 'vegetable' },
{ label: 'Meat', value: 'meat' },
]}
value={food}
onChange={handleFoodChange}
/>
<Dropdown
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>
);
};

That's is it for creating a Dropdown 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.