How to get the value of a dropdown button in ReactJS
Understanding the Core Concept
We are going to talk about dropdown buttons in ReactJS, and how to fetch values from them. You can think of a dropdown button like a box of chocolates, where each chocolate piece represents an option. The whole idea here is to let you select your favorite piece (or option) from the box (or dropdown button).
The Dropdown Button in ReactJS
In ReactJS, a dropdown button is a user interface element that allows users to choose one value from a list. When a dropdown button is inactive, it displays a single value. When activated, it displays a list of values, one of which can be selected.
To create a dropdown button in ReactJS, we use the <select>
HTML tag. Inside the <select>
tag, we place several <option>
tags, each representing a value in the dropdown list.
Let's create a simple dropdown button:
import React from 'react';
export default function App() {
return (
<div className="App">
<select>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
<option value="banana">Banana</option>
</select>
</div>
);
}
Fetching the Value of a Dropdown Button
Now that we have our dropdown button set up, let's see how we can fetch the value selected by the user. To do this, we need to use the onChange
event handler. Think of onChange
as a gatekeeper that notifies you whenever a change occurs in the dropdown button.
Let's add onChange
to our dropdown button:
import React from 'react';
export default function App() {
const handleSelectChange = (event) => {
console.log(event.target.value);
};
return (
<div className="App">
<select onChange={handleSelectChange}>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
<option value="banana">Banana</option>
</select>
</div>
);
}
In this example, handleSelectChange
is a function that logs the value of the selected option to the console whenever the user makes a selection. The event
parameter represents the event that triggered the function, which in this case, is the change in the dropdown button's value.
Storing the Selected Value in a State
In many cases, you'd want to store the selected value for use later in your code. In ReactJS, we use states for this purpose. States are like memory boxes that hold information for us to use later.
We use the useState
hook to create a state in ReactJS. Let's add a state to our component:
import React, { useState } from 'react';
export default function App() {
const [selectedFruit, setSelectedFruit] = useState('');
const handleSelectChange = (event) => {
setSelectedFruit(event.target.value);
};
return (
<div className="App">
<select onChange={handleSelectChange}>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
<option value="banana">Banana</option>
</select>
<p>You selected: {selectedFruit}</p>
</div>
);
}
In this updated code, we have added a state called selectedFruit
, and a function setSelectedFruit
that updates the state. Initially, the state selectedFruit
is empty, but it gets updated with the value of the selected option when the user makes a selection. We then display the selected fruit in a paragraph below the dropdown button.
Conclusion
Just like a magician pulling out a rabbit from a hat, you've now mastered the art of fetching values from a dropdown button in ReactJS. You've not only learnt how to create a dropdown button and fetch the selected value, but also how to store the selected value in a state.
Remember, practice makes perfect. So, keep experimenting with dropdown buttons and state hooks. Before long, you'll be creating magical user interfaces that can remember user selections, and perhaps, even their favorite fruit!