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 uncontrolled to controlled:
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 React's useState Hook and an event handler, we can keep track of the select's value via React's state. 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 function component and pass the necessary props 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><Selectlabel="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 reusable component now. For example, if you would give your select element some CSS style in React, 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><Selectlabel="What do we eat?"options={[{ label: 'Fruit', value: 'fruit' },{ label: 'Vegetable', value: 'vegetable' },{ label: 'Meat', value: 'meat' },]}value={food}onChange={handleFoodChange}/><Selectlabel="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!