I'm trying to get the selected value of the dropdown using React Js.
It is giving the values but is giving the values randomly from the the options.
<select
name="category-select-1"
class="form-select category-select"
id="category-select-1"
value={eventCategory}
onChange={handleEventCategory}>
<option value={"default"}>Category</option>
<option value={"meeting"}>Meeting</option>
<option value={"workhours"}>Work Hours</option>
<option value={"business"}>Business</option>
<option value={"holiday"}>Holiday</option>
<option value={"getTogether"}>Get-Together</option>
<option value={"gifts"}>Gifts</option>
<option value={"birthday"}>Birthday</option>
<option value={"anniversary"}>Anniversary</option>
<option value={"others"}>Others</option>
</select>
Here's how I'm trying to do it.
const [eventCategory, setEventCategory] = useState();
const handleEventCategory = (e)=>{
setEventCategory(e.target.value);
console.log(eventCategory);
}
What is wrong?
How do I get the value of a selected option in a dropdown using React JS?
console.log(eventCategory);outside of the handler function .... as the state updates are more of like async i.e., with state update it re-renders which is the function body gets called with new values